[英]Error boost filtering_ostream when returned from a function
just a quick question regarding boost::iostream::filtering_ostream(). 关于boost :: iostream :: filtering_ostream()的一个简单问题。
I have a function which (internally) creates a shared_ptr to a boost::iostream::filtering_ostream, and returns a shared pointer to a std::ostream. 我有一个函数(内部)创建一个boost :: iostream :: filtering_ostream的shared_ptr,并返回一个指向std :: ostream的共享指针。
Whenever I don't use a compressor in the function, everything seems to work fine, but as soon as I add a compressor, the output file becomes corrupt. 每当我在函数中不使用压缩器时,一切似乎都可以正常工作,但是一旦添加压缩器,输出文件就会损坏。 If I write the text within the 'getOutputStreamComp' function, everything works then also.
如果我在“ getOutputStreamComp”函数中编写文本,那么一切也将正常工作。
The example below just writes some numbers to a file, just as a POC. 下面的示例只是将一些数字作为POC写入文件中。
#include <iostream>
#include <string>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/shared_ptr.hpp>
boost::shared_ptr<std::ostream> getOutputStream(const std::string& fileName)
{
boost::shared_ptr<boost::iostreams::filtering_ostream> out(boost::shared_ptr<boost::iostreams::filtering_ostream>(new boost::iostreams::filtering_ostream()));
out->push(boost::iostreams::file_sink(fileName),std::ofstream::binary);
return out;
}
boost::shared_ptr<std::ostream> getOutputStreamComp(const std::string& fileName)
{
boost::shared_ptr<boost::iostreams::filtering_ostream> out(boost::shared_ptr<boost::iostreams::filtering_ostream>(new boost::iostreams::filtering_ostream()));
out->push(boost::iostreams::gzip_compressor());
out->push(boost::iostreams::file_sink(fileName),std::ofstream::binary);
return out;
}
int main(int argc, char** argv)
{
boost::shared_ptr<std::ostream> outFile = getOutputStream("test.txt");
boost::shared_ptr<std::ostream> outFileComp = getOutputStreamComp("testcomp.txt.gz");
// This file is fine.
for (size_t i(0); i < 10000; ++i)
{
*outFile << "i: " << i << std::endl;
}
// This file is corrupt.
for (size_t i(0); i < 10000; ++i)
{
*outFileComp << "i: " << i << std::endl;
}
}
Any ideas that you might have would be gratefully received! 您可能收到的任何想法将不胜感激!
Thanks, 谢谢,
Dave 戴夫
After fixing the typos ( *outFile
and *outFileComp
), I cannot reproduce this: compiling with g++ 4.8.1/boost-1.53, it runs and produces two good files: test.txt
with 10,000 lines (file size 78,890) and testcomp.txt.gz
(file size 22,064) which unpacks with gunzip into the exact copy of test.txt
. 修正拼写错误(
*outFile
和*outFileComp
)之后,我无法重现此问题:使用g ++ 4.8.1 / boost-1.53进行编译,它将运行并生成两个良好的文件:10,000行(文件大小为78,890)的test.txt
和testcomp.txt.gz
(文件大小为22,064),该文件会用gunzip解压缩到test.txt
的确切副本中。
Perhaps in your real program, you tried examining the file before the program ended (or at least before the last reference to the shared_ptr was gone)? 也许在您的真实程序中,您尝试在程序结束之前(或至少在最后一次对shared_ptr的引用消失之前)检查文件了吗? A common gotcha of compressed filtered streams is that flushing
outFileComp
as you do with std::endl
does not force the complete writeout of the compressed file so far. 压缩过滤流的常见问题是,像使用
std::endl
一样冲洗outFileComp
不会强制到目前为止完全压缩文件的写出。
Okay, so I found the issue, with help from Cubbi - the following line(which appears twice): 好的,所以在Cubbi的帮助下,我找到了问题-以下行(出现两次):
out->push(boost::iostreams::file_sink(fileName),std::ofstream::binary);
should have been: 本来应该:
out->push(boost::iostreams::file_sink(fileName,std::ofstream::binary));
The code was still compiling, so I was probably setting a flag somewhere in the filtering_ostream, which may have led to issues further down the line. 该代码仍在编译中,因此我可能在filtering_ostream中的某个位置设置了一个标志,这可能导致进一步的问题。
This fix allows the file to be created as expected. 此修补程序允许按预期方式创建文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.