原函数:
FILE *freopen(const char *filename, const char *mode, FILE *stream)
函数说明:FILE *freopen(const char *filename, const char *mode, FILE *stream)关联一个新的文件名与给定的打开流,同时关闭旧文件流。
参数:
- filename -- 是C字符串,其中包含要打开的文件名。
- mode -- 是C字符串,其中包含文件访问模式。它包括:
返回值:
如果该文件被重新打开成功,则函数返回一个指针,指向一个对象识别流,否则返回空指针。
如何使用freopen()函数:
#include
int main () {
FILE *fp;
printf("This text is redirected to stdout ");
fp = freopen("file1.txt", "w+", stdout);
printf("This text is redirected to file1.txt ");
fclose(fp);
return 0;
}
编译和运行上面的程序,发送以下行标准输出:
This text is redirected to stdout
freopen()调用后,关联STDOUT文件file1.txt,所以我们在STDOUT 写进入file1.txt。因此,文件file1.txt将有以下内容。
This text is redirected to file1.txt