关于 boost filtering_streams 的一些基本问题。我有几十个函数采用 std::ofstream& 参数
void foo(std::ofstream& outStream)
{
// lots of operations, like this:
outStream << "various bits of text";
}
void StreamSomeTextToFile(char* fileName)
{
ofstream myFileStream(fileName, ios::out | ios::app | ios::binary);
foo(myFileStream);
myFileStream.close();
}
现在我想使用 boost filtering_stream 输出到一个压缩的 ZIP 文件。用于打包和解包的常用 boost filtering_streams 测试代码已编译、链接并完美地为我工作。我想替换过滤流:
void StreamSomeCompressedTextToFile(char* fileName)
{
ofstream myFileStream(destPath, std::ios_base::out | std::ios_base::app | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::output> myCompressedFileStream;
myCompressedFileStream.push(boost::iostreams::zlib_compressor());
myCompressedFileStream.push(myFileStream);
foo(myCompressedFileStream); // I can't just pass myCompressedFileStream to foo(std::ofstream&), right?
myFileStream.close();
}
三个问题:
1) 我以前接受 std::ofstream& outStream 的所有函数现在是否需要接受 boost::iostreams::filtering_streambuf& 类型的参数?或者是否有适当的参数类型,以便那些众多(“foo”)函数可以与任何一种类型的流类型一起使用?
2) 在我的简单测试用例中,我无法将流运算符语法与过滤流缓冲区一起使用:
myCompressedFileStream << "some text";
这产生了错误:'operator<<'不匹配。我同样用 write() 编译错误:
error: 'class boost::iostreams::filtering_streambuf<boost::iostreams::output, char, std::char_traits<char>, std::allocator<char>, boost::iostreams::public_>' has no member named 'write
'
3)在常见的测试用例示例代码(如下)中,我很困惑在创建文件“hello.z”后找不到它。解包代码(也在下面)清楚地引用了它——那么在哪里可以找到它呢?注意:这个位置终于被发现了:它在 /Library/Preferences/
void pack()
{
std::ofstream file("hello.z", std::ios_base::out | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
out.push(boost::iostreams::zlib_compressor());
out.push(file);
char data[5] = {'a', 'b', 'c', 'd', 'e'};
boost::iostreams::copy(boost::iostreams::basic_array_source<char>(data, sizeof(data)), out);
file.close();
}
void unpack()
{
std::fstream file("hello.z", std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(file);
boost::iostreams::copy(in, std::cout);
}
顺便说一句:XCode 3.2.6、GNU 4.0、OS X 10.6.8