freopen

来自cppreference.com
< c‎ | io

定义于头文件 <stdio.h>
FILE *freopen( const char *filename, const char *mode, FILE *stream );
现有的文件流stream重新分配到不同的文件确定由filenameusing指定的模式。 mode使用,以确定新的文件访问模式.
原文:
Reassigns an existing file stream stream to a different file identified by filenameusing specified mode. mode is used to determine the new file access mode.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

目录

[编辑] 参数

filename -
文件名称相关联的文件流
原文:
file name to associate the file stream to
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
mode -
null结尾的字符串,确定新的文件访问模式
File access
mode string
Meaning Explanation Action if file
already exists
Action if file
does not exist
"r" read Open a file for reading read from start failure to open
"w" write Create a file for writing destroy contents create new
"a" append Append to a file write to end create new
"r+" read extended Open a file for read/write read from start error
"w+" write extended Create a file for read/write destroy contents create new
"a+" append extended Open a file for read/write write to end create new
File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has effect only on Windows systems.
On the append file access modes, data is written to the end of the file regardless of the current position of the file position indicator.
原文:
null-terminated character string determining new file access mode
File access
mode string
Meaning Explanation Action if file
already exists
Action if file
does not exist
"r" read Open a file for reading read from start failure to open
"w" write Create a file for writing destroy contents create new
"a" append Append to a file write to end create new
"r+" read extended Open a file for read/write read from start error
"w+" write extended Create a file for read/write destroy contents create new
"a+" append extended Open a file for read/write write to end create new
File access mode flag "b" can optionally be specified to open a file in binary mode. This flag has effect only on Windows systems.
On the append file access modes, data is written to the end of the file regardless of the current position of the file position indicator.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里
stream -
修改的文件流
原文:
the file stream to modify
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 返回值

stream成功,NULL失败
原文:
stream on success, NULL on failure
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 示例

stdout下面的代码重定向到一个文件中
原文:
The following code redirects stdout to a file
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

#include <stdio.h>
 
int main()
{
    printf("stdout is printed to console");
    freopen("redir.txt", "w", stdout);
    printf("Stdout is redirected to a file");
    fclose(stdout);
}

输出:

stdout is printed to console

[编辑] 另请参阅

打开文件
(函数) [edit]
关闭文件
(函数) [edit]
freopenC++文档