title: Program that read file and send it to parent process with pipe
body: <p>I need to write a program that create pipe send filename from command line to child process. In child read that file and send it back using pipe. Parent process should print the file. if error occur in child process error must be send to parent process.</p>
  
  <p>here is my code, it print some junk along file file (and also it disable scrolling in terminal emulator when I run it).</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  void main(int argc, char *argv[]) {
     int pipefd[2];
     char buff[100];
     int childpid;
     int size;
     FILE *file;
  
     if (argc != 2) {
        printf("usage:\n%s &lt;filename&gt;\n", argv[0]);
        exit(1);
     }
     if (pipe(pipefd) &lt; 0) {
         perror("can't open pipe\n");
     }
     if ((childpid = fork()) == 0) {
        sleep(1);
        size = read(pipefd[0], buff, sizeof(buff));
        file = fopen(buff, "r");
        if (file == NULL) {
           write(pipefd[1], "Can't open file", 15);
           exit(1);
        }
        while (!feof(file)) {
           if (fgets(buff, sizeof(buff), file) == NULL) {
              write(pipefd[1], "Error reading file", 18);
           } else {
              write(pipefd[1], buff, sizeof(buff));
           }
        }
     } else if (childpid &gt; 0) {
        size = strlen(argv[1]);
        if (write(pipefd[1], argv[1], size) != size) {
           perror("Error writing to pipe\n");
        }
        wait(NULL);
        while ((size = read(pipefd[0], buff, sizeof(buff))) &gt; 0) {
           write(1, buff, size);
        }
     }
     exit(0);
  }
  </code></pre>