gpt4 book ai didi

c++ - 将进程的输出写入缓冲区

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:05 25 4
gpt4 key购买 nike

Possible Duplicate:
How to execute a command and get output of command within C++?

在我的 C++ 程序中,我使用 execv/system 运行外部程序。

我想将程序的输出重定向到程序中的缓冲区,即:

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

谢谢:-)

最佳答案

我不确定是否还有其他方法可以做到这一点,但这就是我的想法

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 替换为您要从文件中读取的字节数。

关于c++ - 将进程的输出写入缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13181342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com