有一个函数使用FILE*来序列化对象。
此外,我希望以gzip格式序列化对象。
为了做到这一点,我尝试了如下:
boost::shared_ptr<FILE>
openForWriting(const std::string& fileName)
{
boost::shared_ptr<FILE> f(popen(("gzip > " + fileName).c_str(), "wb"), pclose);
return f;
}
boost::shared_ptr<FILE> f = openForWriting(path);
serilizeUsingFILE(f.get());但是这种方法会导致分段故障。
你能帮我了解一下节段故障的原因吗?
发布于 2015-07-08 08:23:19
你有几个问题。
首先,如果传递为NULL,则pclose将出现分段错误。因此,在构建shared_ptr之前,需要从popen中测试null。
其次,popen不以'b‘作为标志,所以类型字符串应该是"w“。
boost::shared_ptr<FILE>
openForWriting(const std::string& fileName)
{
FILE *g = popen(("gzip >" + fileName).c_str(), "w");
if (!g)
return boost::shared_ptr<FILE>();
boost::shared_ptr<FILE> f(g, pclose);
return f;
}https://stackoverflow.com/questions/31286972
复制相似问题