Hoffmann Hoffmann - 1 year ago 126
C Question

How to redirect the output back to the screen after freopen("out.txt", "a", stdout)

#include <stdio.h>

int main() {
printf("This goes to screen\n");
freopen("out.txt", "a", stdout);
printf("This goes to out.txt");
freopen("/dev/stdout", "a", stdout);
printf("This should go to screen too, but doesn't\n");

return 0;

I call freopen to redirect the stdout to out.txt then I print something on the file, now I want to redirect it back to the screen, but freopen("/dev/stdout", "a", stdout); doesn't work. Is there any way to do that using ANSI C or POSIX system calls?

Answer Source

I can't think of a way to do this in a cross-platform manner, but on GNU/Linux systems (and maybe other POSIX-compliant ones, too) you can freopen ("/dev/tty", "a", stdout). Is this what you were trying to do?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download