-
fprintf(stdout, "This will go to the terminal.\n");
-
-
//Save position of current standard output
-
fpos_t pos;
-
fgetpos(stdout, &pos);
-
int fd = dup(fileno(stdout));
-
freopen("/tmp/somefile.txt", "w", stdout);
-
-
fprintf(stdout, "This will go to the file /tmp/somefile.txt.\n");
-
-
//Flush stdout so any buffered messages are delivered
-
fflush(stdout);
-
//Close file and restore standard output to stdout - which should be the terminal
-
dup2(fd, fileno(stdout));
-
close(fd);
-
clearerr(stdout);
-
fsetpos(stdout, &pos);
-
-
fprintf(stdout, "This will go to the terminal.\n");
redirect stdout and back to screen
最新推荐文章于 2024-08-11 13:07:48 发布