0

what do I need to do or add into my code in order for this error to go away. I believe that STDOUT_FILENO is part of the C STDLIB. Thank you. The error is only showing for the first STDOUT_FILENO, not the second one that is the line below it.

#include <stdio.h>
#include <stdlib.h>

void main(int argc, char *argv[]) {

int pipefd[2];

pid_t cpid;

char buffer;

if (pipe(pipefd) == -1)

{
exit(1);

}

cpid = fork();

if (cpid == -1)

{
exit(1);

}

if (cpid == 0) {

close(pipefd[1]); 

while (read(pipefd[0], &buffer, 1) > 0)

write(STDOUT_FILENO, &buffer, 1);

write(STDOUT_FILENO, "\n", 1);

close(pipefd[0]);

exit(EXIT_SUCCESS);

}

else { 

close(pipefd[0]);

write(pipefd[1], argv[1], strlen(argv[1]));

close(pipefd[1]);

wait(NULL); 

exit(EXIT_SUCCESS);

}

}
iueass
  • 9
  • 3
  • 1
    Try including `unistd.h`. [The difference between stdout and STDOUT_FILENO](https://stackoverflow.com/questions/12902627/the-difference-between-stdout-and-stdout-fileno) – Retired Ninja Apr 19 '22 at 02:52
  • 1
    If your implementation has no `unistd.h`, you can set the value in a compatible manner by first checking if it is defined, and if not, definine it as `1`. E.g. `#ifndef STDOUT_FILENO #define STDOUT_FILENO 1 #endif` (where each `#` begins a new line) – David C. Rankin Apr 19 '22 at 04:03
  • 1
    Btw, your code is hard to read due to the formatting you choose. – Allan Wind Apr 19 '22 at 04:10

1 Answers1

4

On POSIX systems STDOUT_FILENO is defined in unistd.h which you include with:

#include <unistd.h>
Allan Wind
  • 23,068
  • 5
  • 28
  • 38