-
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.