std::freopen
STD::freopen
Defined in header | | |
---|---|---|
std::FILE* freopen( const char* filename, const char* mode, std::FILE* stream | | |
首先,尝试关闭与stream
忽略任何错误。那么,如果filename
不为空,则尝试打开filename
使用mode
好像fopen
,并将该文件与stream
.如果filename
为空指针,则该函数尝试重新打开已与stream
%28--在本例中,定义了允许更改模式的实现--%29。
参数
filename | - | file name to associate the file stream to |
---|---|---|
mode | - | 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 no effect on POSIX systems, but on Windows, for example, it disables special handling of '\n' and '\x1A'. 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. |
File access mode string | Meaning | Explanation |
"r" | read | Open a file for reading |
"w" | write | Create a file for writing |
"a" | append | Append to a file |
"r+" | read extended | Open a file for read/write |
"w+" | write extended | Create a file for read/write |
"a+" | append extended | Open a file for read/write |
可以选择指定文件访问模式标志“b”以二进制模式打开文件。此标志对POSIX系统没有影响,但在Windows上,它禁用%27\n%27和%27\x1A%27的特殊处理。在附加文件访问模式上,数据被写入文件的末尾,而不管文件位置指示符的当前位置。
流-要修改的文件流。
返回值
stream
在成功的时候,NULL
在失败的时候。
注记
freopen
是更改流的窄/宽方向的唯一方法,一旦由I/O操作或std::fwide
...
例
以下代码重定向stdout
文件。
二次
#include <cstdio>
int main()
{
std::printf("stdout is printed to console\n"
if(std::freopen("redir.txt", "w", stdout)) {
std::printf("stdout is redirected to a file\n" // this is written to redir.txt
std::fclose(stdout
}
}
二次
产出:
二次
stdout is printed to console
二次
另见
fopen | opens a file (function) |
---|---|
fclose | closes a file (function) |
C.FREOPEN文件
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。