由网友(准备好没)分享简介:我需要对程序的输入执行类似的操作:stream input;if (decompressed)input.open(filepath);else {file_descriptor=_popen("decompressor "+filepath,"r");input.open(file_descriptor);}...![为什么VC安装完之后没有C Source File](https://p.xsw88.cn/allimgs/daicuo/20230903/4934.png)
我需要对程序的输入执行类似的操作:
stream input;
if (decompressed)
input.open(filepath);
else {
file_descriptor=_popen("decompressor "+filepath,"r");
input.open(file_descriptor);
}
input.read(...)
...
我可以看到一种解决方案-在两种情况下都使用_popen,如果文件已经解压缩,则只将文件复制到stdout,但这似乎不是很好。
![为什么VC安装完之后没有C Source File](https://p.xsw88.cn/allimgs/daicuo/20230903/4934.png)
有趣的是,这与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;
}
相关推荐
最新文章