gra*_*raw 4 c++ boost iostream file stream
我需要为我的程序输入执行类似的操作:
stream input;
if (decompressed)
input.open(filepath);
else {
file_descriptor=_popen("decompressor "+filepath,"r");
input.open(file_descriptor);
}
input.read(...)
...
Run Code Online (Sandbox Code Playgroud)
我可以看到一个解决方案 - 在两种情况下都使用_popen,如果它已经解压缩,只需将文件复制到stdout,但这似乎不是很优雅.
有趣的是,与C相比有多难 - 我想标准库错过了它.现在我迷失在神秘的boost :: iostreams文档中.如果有人知道如何,示例代码会很棒.
这就是你所追求的:
#include <cstdio>
#include <string>
#include <iostream>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
namespace io = boost::iostreams;
int main()
{
bool flag = false;
FILE* handle = 0;
if (flag)
{
handle = _popen("dir", "r");
}
else
{
handle = fopen ("main.cpp", "r");
}
io::stream_buffer<io::file_descriptor_source> fpstream (fileno(handle));
std::istream in (&fpstream);
std::string line;
while (in)
{
std::getline (in, line);
std::cout << line << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)