I am redirecting the output of stderr and stdout of my c program to two files and then restoring the original stdout and stderr:
int sout = dup(fileno(stdout
Instead of opening the file again for stderr
,as in:
freopen("test.txt","a",stderr);
redirect it to stdout
at the file descriptor level by doing:
dup2(fileno(stdout), fileno(stderr));
Note that stdout
and stderr
will still use independent user level buffers and, when not directed at an interactive terminal, flushing rules are different. This will most likely be the main cause for different output ordering when redirected. See this explanation of flushing modes and the man page for setvbuf()
.