user-0 提问: 请问如何将标准输出重定向到某个控件上？

回复:
user: user-1
content: 引用 5 楼 sdghchj 的回复:用popen解决了 看懂了？

user: user-0
content: 用popen解决了

user: user-0
content: 引用 3 楼 user-2 的回复:linux不是有fmemopen么 用fmemopen打开一个内存文件，然后用dup2到stdout，然后循环select 监控它，行吗？

user: user-2
content: linux不是有fmemopen么

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:仅供参考：#include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> pid_t rw_popen(char* cmd, FILE **rfile, FILE **wfile) {     int pipefd[2],pipefd2[2]; //管道描述符     pid_t pid; //进程描述符      if (pipe(pipefd) < 0) //建立管道     {         printf("rw_popen() pipe create error/n");         return 0;     }     if (pipe(pipefd2) < 0) //建立管道     {         printf("rw_popen() pipe create error/n");         return 0;     }      pid = fork(); //建立子进程      if (pid < 0)     return 0;      if (0 == pid) //子进程中     {         close(pipefd[0]);         dup2(pipefd[1], 1);         close(pipefd[1]);         dup2(pipefd2[0], 0);         close(pipefd2[0]);         close(pipefd[1]);         char *argv[] = { "/bin/sh", "-c", cmd, NULL };         if (execvp("/bin/sh", argv) < 0) //用exec族函数执行命令         exit(1);     }      close(pipefd[1]);     *rfile = fdopen(pipefd[0], "r");     close(pipefd2[0]);     *wfile = fdopen(pipefd2[1], "w");     return pid; } void rw_pclose(pid_t pid, FILE *rfile, FILE *wfile) {     int status;     waitpid(pid, &status, 0);     fclose(rfile);     fclose(wfile); } int main() {     char buf1[1024];     FILE *file1, *file2;     pid_t pid;     pid = rw_popen("sh", &file1, &file2);     if (pid) {         fputs("pwd;exit;\n",file2);         fflush(file2);         if (fgets(buf1, 1400, file1)) {             puts(buf1);         }         rw_pclose(pid, file1, file2);     }     return 1; }  哎，看不懂赵老师的意思

user: user-1
content: 仅供参考：#include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> pid_t rw_popen(char* cmd, FILE **rfile, FILE **wfile) {     int pipefd[2],pipefd2[2]; //管道描述符     pid_t pid; //进程描述符      if (pipe(pipefd) < 0) //建立管道     {         printf("rw_popen() pipe create error/n");         return 0;     }     if (pipe(pipefd2) < 0) //建立管道     {         printf("rw_popen() pipe create error/n");         return 0;     }      pid = fork(); //建立子进程      if (pid < 0)     return 0;      if (0 == pid) //子进程中     {         close(pipefd[0]);         dup2(pipefd[1], 1);         close(pipefd[1]);         dup2(pipefd2[0], 0);         close(pipefd2[0]);         close(pipefd[1]);         char *argv[] = { "/bin/sh", "-c", cmd, NULL };         if (execvp("/bin/sh", argv) < 0) //用exec族函数执行命令         exit(1);     }      close(pipefd[1]);     *rfile = fdopen(pipefd[0], "r");     close(pipefd2[0]);     *wfile = fdopen(pipefd2[1], "w");     return pid; } void rw_pclose(pid_t pid, FILE *rfile, FILE *wfile) {     int status;     waitpid(pid, &status, 0);     fclose(rfile);     fclose(wfile); } int main() {     char buf1[1024];     FILE *file1, *file2;     pid_t pid;     pid = rw_popen("sh", &file1, &file2);     if (pid) {         fputs("pwd;exit;\n",file2);         fflush(file2);         if (fgets(buf1, 1400, file1)) {             puts(buf1);         }         rw_pclose(pid, file1, file2);     }     return 1; }