【发布时间】:2017-09-20 12:21:52
【问题描述】:
我在使用“boost/iostreams/filtering_stream.hpp”时使用 filetering_istream 类型将信息保存在解压缩文件中。但我想将其转换为 ifstream 类型。它有什么办法吗?非常感谢!
代码如下:
#include <istream>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
int main(){
std::ifstream file("test_data.dat.gz");
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::gzip_decompressor());
in.push(file);
/* add code to convert filtering_istream 'in' into ifstream 'pfile' */
/* It seems that the following code returns a pointer NULL */
// std::ifstream* pfile = in.component<std::ifstream>(1);
return 0;
}
在尝试了 zett42 提出的 boost::ref 和 boost::wrapper 之后,ifstream 确实有效。唯一的问题是它没有给出想要的短语。
在我的 .gz 文件中,我写道:
THIS IS A DATA FILE!
8 plus 8 is 16
但是使用 ifstream,我得到了:
is_open: 1
\213<\373Xtest_data.dat\361\360V"G\307G7OWE.\205\202\234\322b\205\314bC3.\327+>\314$
我不确定这里发生了什么,我可以做些什么来恢复它吗?
【问题讨论】:
-
如果您使用
ifstream,您将读取压缩数据。也许我完全误解了你的问题。如果要读取未压缩的数据,只需从in读取。那么就不需要“投射”任何东西了。
标签: c++ boost casting iostream ifstream