Code below redirects stdout to a file fname & then redirects back to original stdout. It works fine for me.
But I am not able to understand how it actually works. If anyone can help me understand I will appreiciate it.
printf("\n This is console");
fflush(stdout);
fgetpos(stdout, &pos);
fd = dup(fileno(stdout));
freopen(fname, "a+", stdout);
printf("inside file op");
fflush(stdout);
dup2(fd,fileno(stdout));
close(fd);
clearerr(stdout);
fsetpos(stdout, &pos);
printf("\nBack to Console");
Let’s go through it line by line. The first line prints something to
stdout:Then it flushes
stdoutso all the remaining data in the buffer gets sent tostdoutand won’t get mixed up with the file data:Now we store the current position of ourselves in
stdoutbecause otherwise ifstdoutwas already directed to a file, we might (?) overwrite earlier parts of it.Now we clone the file descriptor of what’s currently
stdout. Since we’re about to change wherestdoutpoints to, we need to keep a copy of the original:Now that we have everything preserved, we can reopen
stdoutas the file:At this point,
stdouthas been redirected to the file. We can now print to it:Now we’re done printing to the file. We need to flush
stdout(now the file) so it doesn’t get mixed up with the normalstdoutdata:After that, we clone the original
stdoutfile descriptor over the currentstdoutdescriptor.The cloned one can be closed now:
I’m not quite sure why this is here but this clears any errors that occurred writing to the file:
Now we restore our position in
stdout. Again, as far as I know, this is only useful if it was originally redirected to a file:Now we’re back to the original
stdout, so we can print again: