freopen's feature in Visual Studio

freopen's feature in Visual Studio

If you close with \'fclose\' function standard stream (stdout, stderr) redirected with freopen, Then when next fopen will be called it will return handler to new opened file and standard stream will be linked to it to. This behaviour is discovered in Visual Studio v6,v7.

Example:

#include "stdio.h"
main()
{
char *szA = "a.txt";
char *szB = "b.txt";
FILE *outf1,*outf2;

outf1 = freopen(szA, "a+t",stderr);
fputs("1. this record will be in a.txt",outf1);
fputs("2. this record will be in a.txt too",outf1);
fclose(outf1);

outf2 = fopen(szB, "wt");
fputs("3. this record will be in b.txt", outf2);
fputs("4. this record will be in b.txt. But its not obvious!!!", stderr);
fclose(outf2);
}