C语言 从 exec 获取输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7292642/
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
Grabbing output from exec
提问by TrewTzu
so im trying to write some thing that needs to grab command output and then ill be treating it and then passing it to another program.
所以我试图写一些需要获取命令输出然后处理它然后将它传递给另一个程序的东西。
but im having an issue, i cant work out how to get the command output and store it below is a sample of what i have
但我有一个问题,我无法弄清楚如何获取命令输出并将其存储在下面是我所拥有的示例
if(fork() == 0){
execl("/bin/ls", "ls", "-1", (char *)0);
/* hopefully do something with the output here*/
}else{
*other stuff goes here*
}`
so basically im wondering if there is any way i can get the output from the "execl" and pass it to some thing else (e.g. via storing it in some kind of buffer).
所以基本上我想知道是否有任何方法可以从“execl”获取输出并将其传递给其他东西(例如,通过将其存储在某种缓冲区中)。
any suggestions would be great. thanks guys.. `
任何建议都会很棒。谢谢你们..`
回答by Aif
You have to create a pipe from the parent process to the child, using pipe().
Then you must redirect standard ouput(STDOUT_FILENO) and error output(STDERR_FILENO) using dupor dup2to the pipe, and in the parent process, read from the pipe.
It should work.
您必须创建一个从父进程到子进程的管道,使用pipe(). 然后,您必须使用或将standard ouput(STDOUT_FILENO) 和error output(STDERR_FILENO)重定向到管道,并在父进程中从管道中读取。它应该工作。dupdup2
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define die(e) do { fprintf(stderr, "%s\n", e); exit(EXIT_FAILURE); } while (0);
int main() {
int link[2];
pid_t pid;
char foo[4096];
if (pipe(link)==-1)
die("pipe");
if ((pid = fork()) == -1)
die("fork");
if(pid == 0) {
dup2 (link[1], STDOUT_FILENO);
close(link[0]);
close(link[1]);
execl("/bin/ls", "ls", "-1", (char *)0);
die("execl");
} else {
close(link[1]);
int nbytes = read(link[0], foo, sizeof(foo));
printf("Output: (%.*s)\n", nbytes, foo);
wait(NULL);
}
return 0;
}
回答by Darcy Rayner
Open a pipe, and change stdout to match that pipe.
打开一个管道,并更改标准输出以匹配该管道。
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int pipes[2];
pipe(pipes); // Create the pipes
dup2(pipe[1],1); // Set the pipe up to standard output
After that, anything which goes to stdout,(such as through printf), comes out pipe[0].
之后,任何进入 stdout 的东西(例如通过 printf),都会从 pipe[0] 出来。
FILE *input = fdopen(pipe[0],"r");
Now you can read the output like a normal file descriptor. For more details, look at this
现在您可以像读取普通文件描述符一样读取输出。有关更多详细信息,请查看此
回答by user2420449
Thanks Jonathan Leffler, and i optimize the above code for it can't read all response for one time.
感谢 Jonathan Leffler,我优化了上面的代码,因为它一次无法读取所有响应。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#define die(e) do { fprintf(stderr, "%s\n", e); exit(EXIT_FAILURE); } while (0);
int main() {
int link[2];
pid_t pid;
char foo[4096 + 1];
memset(foo, 0, 4096);
if (pipe(link)==-1)
die("pipe");
if ((pid = fork()) == -1)
die("fork");
if(pid == 0) {
dup2 (link[1], STDOUT_FILENO);
close(link[0]);
close(link[1]);
execl("/bin/ls", "ls", "-1", (char *)0);
die("execl");
} else {
close(link[1]);
int nbytes = 0;
std::string totalStr;
while(0 != (nbytes = read(link[0], foo, sizeof(foo)))) {
totalStr = totalStr + foo;
printf("Output: (%.*s)\n", nbytes, foo);
memset(foo, 0, 4096);
}
wait(NULL);
}
return 0;
}

