I want to redirect STDOUT to a file on the disk. The point is to make the printf on my program write to a file instead of the console.
I saw some articles on the web where they used:
Bad file descriptor
STDOUT_FILENO is a numeric file descriptor (usually
1). When you use
close, you release the descriptor, but then reassign it with
dup2. Any output to that descriptor will now go to the new file.
stdout on the other hand is a
FILE*, an object of sorts that contains a file descriptor. Doing
printf formats output into a buffer associated with the
FILE, and then
writes the buffer to the descriptor (depending upon the buffering mode). When you
FILE*, it (normally)
closes the underlying descriptor, and frees the
FILE object. The
dup2 does not ressurect