在 C++ 中读取 popen 结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7807755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading popen results in C++
提问by Stefano
I am writing a C++ application and I need to read the result of a system command.
我正在编写一个 C++ 应用程序,我需要读取系统命令的结果。
I am using popen()
more or less as shown here:
我popen()
或多或少地使用如下所示:
const int MAX_BUFFER = 2048;
string cmd="ls -l";
char buffer[MAX_BUFFER];
FILE *stream = popen(cmd.c_str(), "r");
if (stream){
while (!feof(stream))
{
if (fgets(buffer, MAX_BUFFER, stream) != NULL)
{
//here is all my code
}
}
pclose(stream);
}
I've been trying to re-write this in a different way. I saw some non-standard solutions like:
我一直在尝试以不同的方式重新编写它。我看到了一些非标准的解决方案,例如:
FILE *myfile;
std::fstream fileStream(myfile);
std::string mystring;
while(std::getline(myfile,mystring))
{
// .... Here I do what I need
}
My compiler does not accept this though.
我的编译器不接受这一点。
How can I read from popen
in C++?
我如何从popen
C++ 中读取?
回答by Flexo
Your example:
你的例子:
FILE *myfile;
std::fstream fileStream(myfile);
std::string mystring;
while(std::getline(myfile,mystring))
Does't work because although you're very close the standard library doesn't provide an fstream
that can be constructed from a FILE*
. Boost iostreamsdoes however provide an iostream
that can be constructed from a file descriptor and you can get one from a FILE*
by calling fileno
.
不起作用,因为尽管您非常接近,但标准库不提供fstream
可以从FILE*
. 但是,Boost iostreams确实提供了iostream
可以从文件描述符构造的 ,并且您可以FILE*
通过调用fileno
.
E.g.:
例如:
typedef boost::iostreams::stream<boost::iostreams::file_descriptor_sink>
boost_stream;
FILE *myfile;
// make sure to popen and it succeeds
boost_stream stream(fileno(myfile));
stream.set_auto_close(false); // https://svn.boost.org/trac/boost/ticket/3517
std::string mystring;
while(std::getline(stream,mystring))
Don't forget to pclose
later still.
别忘了pclose
以后还是。
Note: Newer versions of boost have deprecated the constructor which takes just a fd
. Instead you need to pass one of boost::iostreams::never_close_handle
or boost::iostreams::close_handle
as a mandatory second argument to the constructor.
注意:较新版本的 boost 已经弃用了只需要一个fd
. 相反,您需要将其中一个boost::iostreams::never_close_handle
或boost::iostreams::close_handle
作为强制的第二个参数传递给构造函数。
回答by Avinash
Here is something which i wrote long back, may help you. It might have some errors.
这是我很久以前写的东西,可能对你有帮助。它可能有一些错误。
#include <vector>
#include <string>
#include <stdio.h>
#include <iostream>
bool my_popen (const std::string& cmd,std::vector<std::string>& out ) {
bool ret_boolValue = true;
FILE* fp;
const int SIZEBUF = 1234;
char buf [SIZEBUF];
out = std::vector<std::string> ();
if ((fp = popen(cmd.c_str (), "r")) == NULL) {
return false;
}
std::string cur_string = "";
while (fgets(buf, sizeof (buf), fp)) {
cur_string += buf;
}
out.push_back (cur_string.substr (0, cur_string.size () - 1));
pclose(fp);
return true;
}
int main ( int argc, char **argv) {
std::vector<std::string> output;
my_popen("ls -l > /dev/null ", output);
for ( std::vector<std::string>::iterator itr = output.begin();
itr != output.end();
++itr) {
std::cout << *itr << std::endl;
}
}