c – 如何使boost :: iostream以与std :: ios :: binary相当的模
我对boost :: iostreams有以下问题.如果有人熟悉编写过滤器,我会非常感谢您的建议/帮助.
我正在编写一对multichar过滤器,它与boost :: iostream :: filtering_stream一起用作数据压缩器和解压缩器. 有几个词,我的压缩器将数据分成数据包,这些数据包是单独编码的,然后刷新到我的文件中. 当我必须从我的文件中恢复数据时(在编程方面,接收一个read(byte_count)请求),我必须读取一个完整的压缩块,缓冲它,解压缩它,然后只给出所需的字节数.我已经实现了这个逻辑,但是现在我正在努力解决以下问题: 打包我的数据时,输出文件中可以显示任何符号.我在阅读文件时遇到麻烦,该文件包含使用boost :: iostreams :: read(….,size)的符号(hex 1A,char 26). 例如,如果我使用std :: ifstream,我会设置一个std :: ios :: binary模式,然后可以简单地读取这个符号. 在实现使用boost :: iostream :: read例程读取char序列的boost :: iostream过滤器时,是否有任何方法可以实现相同的目的? 一些代码在这里: // Compression // ----------- filtering_ostream out; out.push(my_compressor()); out.push(file_sink("file.out")); // Compress the 'file.in' to 'file.out' std::ifstream stream("file.in"); out << stream.rdbuf(); // Decompression // ------------- filtering_istream in; in.push(my_decompressor()); in.push(file_source("file.out")); std::string res; while (in) { std::string t; // My decompressor wants to retrieve the full block from input (say,4096 bytes) // but instead retrieves 150 bytes because meets '1A' char in the char sequence // That obviously happens because file should be read as a binary one,but // how do I state that? std::getline(in,t); // <--------- The error happens here res += t; } 解决方法
读取文件为二进制文件的简答:
打开文件流时指定ios_base :: binary. MSDN Link (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |