发布网友 发布时间:2022-04-22 03:27
共1个回答
热心网友 时间:2023-07-09 07:45
p()函数可以复制一个文件句柄,你可以用p()函数保存对应于stdout标准流的文件句柄。fdopen()函数可以打开一个已用p()函数复制了的流。这样,你就可以重定向并恢复标准流,请看下例:
#include <stdio.h>
void main(void);
void main(void){int orig-stdout;
/ * Duplicate the stdout file handle and store it in orig_stdout. */
orig_stdout = p (fileno (stdout));
/ * This text appears on-screen. * /
printf("Writing to original stdout... \n") ;
/ * Reopen stdout and redirect it to the "redir. txt" file. * /
freopen("redir.txt", "w", stdout);
/ * This text appears in the "redir. txt" file. * /
printf("Writing to redirected stdout.., \n");
/* Close the redirected stdout. * /
fclose (stdout);