How to put stdout back to default after redirecting

Hello All
I am trying to redirect the stdout to a file “file.txt” , by using the command
FILE * fp;

fp = freopen("file.txt","w",stdout) ;

and as I expected it sent all the output data to “file.txt” ,

but my problem now is that how do I change stdout back to default , which means I want stdout outputing data to my Console.

Thanks All

Ed

Just a guess, but what happens if you fclose(fp)?

Maybe this works, but I am not sure:

[code]FILE* fp = fopen(“file.txt”, “a”);

FILE* old_stdout = stdout;
stdout = fp;

printf(“xxx”); // should go to file.txt

fclose(fp);

stdout = old_stdout;

printf(“yyy”); // should go to console[/code]

Regards,
Albrecht

hello
Thanks for reply , actually I did it after I posted and here is how I did it …

int fd;
fpos_t pos;

fflush(stdout); // flush output stream

fgetpos(stdout,&pos); // get position of output stream
 
fd=dup(fileno(stdout));  // duplicate output stream fd=stdout
 
// re-direct output stream (stdout) to the file "filename"

freopen(filename,"w",stdout);

… here everything should go to File.txt

fflush(stdout);
 
dup2(fd,fileno(stdout));
 
close(fd);
 
clearerr(stdout);
 
// restore position of output stream (stdout) to normal 

fsetpos(stdout,&pos);

… here it is back to console

regards

Ed