简体   繁体   English

从函数返回时错误提升boosting_ostream

[英]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.txttestcomp.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.

相关问题 使用boost :: iostreams :: zlib_compressor为什么需要销毁boost :: iostream :: filtering_ostream才能写入接收器? - Why boost::iostream::filtering_ostream using boost::iostreams::zlib_compressor needs to be destroyed for the sink to be written? 在boost iostream filtering_ostream中,sync(),strict_sync()和flush()之间有什么区别? - in boost iostream filtering_ostream, what is the difference between sync(), strict_sync() and flush()? 当我使用“boost :: log :: add_file_log()”函数时,“error ld返回1退出状态” - “error ld returned 1 exit status” when i use “boost::log::add_file_log()” function 当没有从函数显式返回值时,如何强制编译器错误 - How to force compiler error when no value is explicitly returned from function Boost函数:存储boost :: bind返回的任何函数? - Boost function: store any function returned by boost::bind? 通过boost从流中读取xml时出错 - error when read xml from a stream by boost 从头开始构建时出现 Boost 编译错误 - Boost compilation error when building from scratch C ++ Boost-iostream:包含filtering_streambuf.hpp时出现的神秘错误 - C++ Boost-iostream: mysterious error when including filtering_streambuf.hpp 使用 c++ boost::python 从 python 函数返回的列表中获取数据? - Get data from a list returned by a python function using c++ boost::python? boost::interprocess::file_lock 与 std::ostream 一起使用时的错误行为 - boost::interprocess::file_lock erroneous behavior when used with std::ostream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM