将标准输出重定向到C中的管道

将标准输出重定向到C中的管道,c,redirect,fork,pipe,stdout,C,Redirect,Fork,Pipe,Stdout,下面是我正在尝试制作的一个程序: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <string.h> int main(int argc, char* argv[]) { char* arguments[] = {"superabundantes.py", NULL}; int

下面是我正在尝试制作的一个程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>



int main(int argc, char* argv[])
{
    char* arguments[] = {"superabundantes.py", NULL};

    int my_pipe[2];
    if(pipe(my_pipe) == -1)
    {
        fprintf(stderr, "Error creating pipe\n");
    }

    pid_t child_id;
    child_id = fork();
    if(child_id == -1)
    {
        fprintf(stderr, "Fork error\n");
    }
    if(child_id == 0) // child process
    {
        close(my_pipe[0]); // child doesn't read
        dup2(my_pipe[1], 1); // redirect stdout

        execvp("cat", arguments);

        fprintf(stderr, "Exec failed\n");
    }
    else
    {
        close(my_pipe[1]); // parent doesn't write

        char reading_buf[1];
        while(read(my_pipe[0], reading_buf, 1) > 0)
        {
            write(1, reading_buf, 1); // 1 -> stdout
        }
        close(my_pipe[0]);
        wait();
    }
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
char*参数[]={“superfundates.py”,NULL};
int my_管道[2];
如果(管道(我的管道)=-1)
{
fprintf(stderr,“创建管道时出错”);
}
pid_t child_id;
child_id=fork();
如果(子项id==-1)
{
fprintf(stderr,“Fork error\n”);
}
if(child_id==0)//子进程
{
关闭(my_管道[0]);//孩子不读书
dup2(my_管道[1],1);//重定向标准输出
execvp(“猫”,参数);
fprintf(stderr,“Exec失败\n”);
}
其他的
{
关闭(my_pipe[1]);//家长不写
字符读取_buf[1];
while(读取(my_pipe[0],读取_buf,1)>0)
{
写(1,reading_buf,1);//1->stdout
}
关闭(my_管道[0]);
等待();
}
}
我想在子对象中执行exec,将子对象的stdout重定向到父对象(通过管道)。我认为问题可能与dup2有关,但我以前没有使用过它。

调用exec时,还需要提供
argv[0]
。所以你的论点应该是:

char* arguments[] = {"cat", "superabundantes.py", NULL};
有什么想法吗?

*/

请指定“问题”是什么,而不是仅仅为了让我们知道而转储代码。如果您不知道问题是什么,请向程序中添加错误报告。不要解决问题,但必须指定int*sts for wait()函数(可以为NULL)。谢谢!更改char*arguments[]={“cat”,“superfundates.py”,NULL}后工作正常;是的!你说得对。char*arguments[]={“cat”,“superfundates.py”,NULL};execvp(argv[0],arguments);工作正常,谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>



int main(int argc, char* argv[])
{
    //char* arguments[] = {"cat","tricky.txt", NULL};
    char* arguments[] = {"./son1", NULL};
    int my_pipe[2];
    if(pipe(my_pipe) == -1)
    {
       fprintf(stderr, "Error creating pipe\n");
    }

    pid_t child_id;
    child_id = fork();
    if(child_id == -1)
    {
        fprintf(stderr, "Fork error\n");
    }
    if(child_id == 0) // child process
    {
        close(my_pipe[0]); // child doesn't read
        dup2(my_pipe[1], 1); // redirect stdout

        execvp(arguments[0], arguments);

        fprintf(stderr, "Exec failed\n");
    }
    else
    {
        close(my_pipe[1]); // parent doesn't write

        char reading_buf[1];

        while(read(my_pipe[0], reading_buf, 1) > 0)
        {
           write(1, reading_buf, 1); // 1 -> stdout
        }

        close(my_pipe[0]);
        wait();
   }
void main()
{
    printf( "***** son run *****\n\r" );
    return;
    while(1);
}