龙空技术网

C语言库函数之freopen()详解

编程基础知识分享 51

前言:

而今小伙伴们对“c语言mode是什么意思”大致比较注意,我们都需要剖析一些“c语言mode是什么意思”的相关文章。那么小编在网摘上收集了一些对于“c语言mode是什么意思””的相关内容,希望我们能喜欢,兄弟们一起来了解一下吧!

原函数:

FILE *freopen(const char *filename, const char *mode, FILE *stream)

函数说明:FILE *freopen(const char *filename, const char *mode, FILE *stream)关联一个新的文件名与给定的打开流,同时关闭旧文件流。

参数:

filename -- 是C字符串,其中包含要打开的文件名。mode -- 是C字符串,其中包含文件访问模式。它包括:stream -- 这是一个文件对象的指针标识重新打开流。

返回值:

如果该文件被重新打开成功,则函数返回一个指针,指向一个对象识别流,否则返回空指针。

如何使用freopen()函数:

#include <stdio.h>

int main () {

FILE *fp;

printf("This text is redirected to stdout \n");

fp = freopen("file1.txt", "w+", stdout);

printf("This text is redirected to file1.txt \n");

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

标签: #c语言mode是什么意思 #c语言文件输入输出freopen如何操作 #c文件输入输出 freopen