//函数名: freopen
//功 能: 实现数据重定向到文件中
//用 法: FILE *freopen(const char *filename, const char *mode, FILE *stream);
//返回值: 成功,则返回文件指针;失败,返回NULL(可以不使用它的返回值) 7
#include <stdio.h>
int main(void)
{
FILE *stream;
/* redirect standard output to a file */
if ((stream = freopen("log.txt", "w", stdout)) == NULL)
{
fprintf(stderr, "error redirecting stdout\n");
return -1;
}
/* this output will go to a file */
printf("This will go into a file.");
stream = freopen("/dev/tty","w",stdout);
/*stdout是向程序的末尾的控制台重定向*/
printf("And now back to the console once again\n");
/* close the standard output stream */
fclose(stdout);
return 0;
}
linux freopen实现数据重定向到文件中
于 2022-11-24 11:57:30 首次发布