How can I copy the number of bytes of the output before piping in C?

176 Views Asked by At

I have a C program that can pipe multiples processes together here's a running example:

./a.out cat test ; grep left ; wc -c

It basically pipes the output of cat test to grep left and grep left to wc -c

I want to add a function that will copy the number of bytes of the output in a .txt file before piping the output. this is the way I pipe the output and the code for the file that I tried:

      int bytes;
      for(bytes = 0; getc(stdout) != EOF; ++bytes);
      
      FILE *f=fopen("count","w");
      fprintf(f, "%d : %d\n", i, bytes);
      fclose(f);
      //stdout
      if (execlp("/bin/sh","sh","-c",tabCommande[i], (char *)NULL) == -1) {
            error_and_exit();
      }

After using my code it only writes the last output in the file but only rights 0 for example. If I pipe 3 processes, it will write 3 : 0

If someone can help me find the issue I would really appreciate it.

0

There are 0 best solutions below