I want to reopen the stdin and stdout (and perhaps stderr while I\'m at it) filehandles, so that future calls to printf()
Why use freopen()? The C89 specification has the answer in one of the endnotes for the section on :
116. The primary use of the
freopenfunction is to change the file associated with a standard text stream (stderr,stdin, orstdout), as those identifiers need not be modifiable lvalues to which the value returned by thefopenfunction may be assigned.
freopen is commonly misused, e.g. stdin = freopen("newin", "r", stdin);. This is no more portable than fclose(stdin); stdin = fopen("newin", "r");. Both expressions attempt to assign to stdin, which is not guaranteed to be assignable.
The right way to use freopen is to omit the assignment: freopen("newin", "r", stdin);