Issue
If someone closes a process' STDOUT
in its file descriptor table like so:
close(STDOUT);
and then immediately opens a file to read/write :
int fd = open("myFile", O_RDWR);
and after that uses printf
:
printf("hello");
I know it won't go to the screen, but will it be printed in the file? If it doesn't, do you have to use fprintf
or the write
system call instead?
Solution
From the man page of open:
The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process.
When you close the file descriptor for STDOUT, the subsequent open system call will assign the fd of stdout to the new file. Printf just sends to the fd it doesn't matter whether it is stdout or not. So a printf in this scenario will dump the output to "myfile"
Answered By - narayana janardhan Answer Checked By - Candace Johnson (WPSolving Volunteer)