My naive solution would be as follows: Create a zipping file buffer and add this buffer to a ostream. But in the example code 'output.z' has always a size of 0 byte.
#include "boost/iostreams/filtering_streambuf.hpp"
#include "boost/iostreams/filter/zlib.hpp"
#include <iostream>
#include <fstream>
struct MyStruct
{
MyStruct() : a(3), b("BlaBla"), c(4) {}
int a;
const char* b;
int c;
};
std::ostream& operator << ( std::ostream& out, const MyStruct& in )
{
return out << in.a << std::endl << in.b << std::endl << in.c;
}
int main ( int argc, char* argv[] )
{
try {
{
boost::iostreams::filtering_ostreambuf sb;
sb.push (boost::iostreams::zlib_compressor());
std::string file("output.z");
std::ofstream device;
device.open (file.c_str(), std::ios_base::binary);
if ( device.fail() )
throw std::runtime_error ( std::string("error opening ") + file );
sb.push (device);
std::ostream ls(&sb);
MyStruct bla;
ls << bla;
}
}
catch(boost::iostreams::zlib_error& e)
{
std::cout << "Zib error\n";
}
catch(...)
{
std::cout << "Any other error\n";
}
}
Where is the conceptual error in this example?
Regards, Gert