Reputation: 1141
There is a function which takes FILE*
to serialize the object.
In addition I want to serialize object in gzip format.
To do this I have try this:
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());
But this approach results to segfault.
Can you please help me to understand the cause of segfault?
Upvotes: 0
Views: 310
Reputation: 1067
You have a couple of problems.
Firstly, pclose will segfault if you pass it NULL. So you need to test for null from popen before you construct the shared_ptr.
Secondly, popen doesn't take 'b' as a flag, so the type string should just be "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;
}
Upvotes: 2