将stdout重定向到没有ANSI警告的文件
我一直试图让程序的STDOUT重定向到文件。 到目前为止,此代码运行良好:
FILE *output = fopen("output","w"); if (dup2(fileno(output),1) == -1) { /* An error occured. */ exit(EXIT_FAILURE); }
问题是,我试图坚持使用ANSI C,而fileno不是ANSI。 当我使用gcc编译时,我收到警告:
gcc -Wall -ansi -pedantic warning: implicit declaration of function 'fileno'
有没有办法将STDOUT重定向到ansi C中的文件?
ANSI C的方法是freopen()
:
if (freopen("output", "w", stdin) == NULL) { /* error occured */ perror("freopen"); }