I've got a Windows Win32/GUI application that sometimes prints interesting output to both stdout
and stderr
, so what I'd like to do is capture that output into a file for review after the application has exited.
The problem is, I can successfully call freopen_s()
to capture stdout
output into a file, or I can use it to capture stderr
output into a file, but trying to do both at once yields only a truncated file with munged data.
Below is a simple program that reproduces the problem; if I comment out either one of the two freopen_s()
calls, then I get the expected output in the blah.txt
file that is created (i.e. one of the two text lines), but what I'd like is to end up with a blah.txt
containing both text lines.
Is that possible under Windows? I could fall back to creating two different files (e.g. blah_stdout.txt
and blah_stderr.txt
) but I'd rather not, since then I'd have to manually reconstruct the relative order in which stdout and stderr output was generated.
int main(int argc, char *argv[])
{
const char * outFileName = "blah.txt";
FILE * junk1 = NULL, junk2 = NULL;
if (freopen_s(&junk1, outFileName, "w", stdout) != 0) abort();
if (freopen_s(&junk2, outFileName , "w", stderr) != 0) abort();
printf("This text was printed to stdout and should appear in blah.txt\n");
fprintf(stderr, "This text was printed to stderr and should appear in blah.txt\n");
return 0;
}