2012-11-01 57 views
0

可能重复:
How to execute a command and get output of command within C++?撰写过程的输出缓冲器

在我的C++程序,我正在使用execv /系统的外部程序。

我想程序的一个缓冲区输出重定向在我的程序,即:

char* buff[some size large enough for any possible output]; 
system(some program); 
//THE PROGRAM I JUST RAN IS A SPECIAL BLACK BOX PROGRAM THAT RUNS 
//AND PARSE AN HTTP DUMP FILE AND PRINTS IT TO THE SCREEN WHEN RUNNING 
//I WANT THE OUTPUT TO GO A BUFFER IN MY PROGRAM. 
//THE IDEA IS TO HAVE THE DATA OF THE OUTPUT ON THE BUFFER RATHER THAN ON A FILE 

//have the buffer filled with the output of the program 

感谢:-)

+3

下次搜索:http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get- output-of-command-within-c –

回答

0

我不知道是否有一些其他的方式做这个,但这是我想到的

char* buff[some size large enough for any possible output]; 
system(some program >somefile.txt); 

//have the buffer filled with the output of the program 
// And redirect it into a file 


//Do initial validity checks and open the file 



read(somefile.txt,buff,bufflen); 
//Read the file 

unlink somefile.txt 
//Unlink if the file is not needed 

用你想从文件中读取的字节数替换bufflen。

+0

或者甚至只是在流中读取它并将其转换为字符串,然后您不必担心大小(如果您要编写一个像这样的外部文件) – Caribou