Pipeing stdin to stdout

Discussion in 'Mac Programming' started by MacDonaldsd, Mar 30, 2007.

  1. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    How would I go about piping the stdout of a child process to the stdin of the parent process ?

    Ive got it working for fixed data lengths below

    main()
    {
    int pfd[2], n;
    pid_t cpid;
    char *msg= "This is a message\n";
    char buf[30];
    pipe(pfd);
    if((cpid= fork()) == -1)
    {
    perror("fork");

    }

    if(cpid== 0)
    {
    close(pfd[0]);

    write(pfd[1], msg, strlen(msg));

    }
    else
    {

    close(pfd[1]);

    n = read(pfd[0], buf, sizeof(buf));
    printf("Received: %s", buf);
    }
    }

    I need to use an exec function in the child, so it will die and therefore I can't save it in a buffer etc So I have to link the stdout of the child to the stdin of the parent.
     

Share This Page