-
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 发布
本文介绍了一种在C语言中将标准输出(stdout)临时重定向到文件的方法,并演示了如何保存当前标准输出的位置、进行重定向、写入文件以及之后如何恢复到初始状态。
617

被折叠的 条评论
为什么被折叠?



