我需要为我的程序输入做这样的事情:
stream input;
if (decompressed)
input.open(filepath);
else {
file_descriptor=_popen("decompressor "+filepath,"r");
input.open(file_descriptor);
}
input.read(...)
...
我可以看到一种解决方案 - 在两种情况下都使用 _popen ,如果文件已经解压,则将文件复制到标准输出,但这看起来不太优雅。
有趣的是,与 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)