©
本文档使用
php.cn手册 发布
在头文件<stdio.h>中定义 | ||
---|---|---|
(1) | ||
FILE * freopen(const char * filename,const char * mode,FILE * stream); | (直到C99) | |
FILE * freopen(const char *限制文件名,const char *限制模式,FILE *限制流); | (自C99以来) | |
errno_t freopen_s(FILE * restrict * restrict newstreamptr,const char * restrict filename,const char * restrict mode,FILE * restrict stream); | (2) | (自C11以来) |
1)首先,试图关闭与之相关的文件stream
,忽略任何错误。然后,如果filename
不为null,则尝试打开通过filename
使用mode
as 指定的文件fopen
,并将该文件与指向的文件流相关联stream
。如果filename
是空指针,那么函数将尝试重新打开已经关联的文件stream
(在此情况下,它是实现定义允许哪些模式更改)。
2)与(1)相同,不同之处在于mode
,处理方式如下,fopen_s
指向文件流的指针被写入,newstreamptr
并且在运行时检测到以下错误并调用当前安装的约束处理函数:
newstreamptr
是一个空指针
stream
是一个空指针
mode
是一个空指针
作为所有的边界检查函数,freopen_s
只能保证__STDC_LIB_EXT1__
是由实现定义的,并且如果用户在包含之前定义__STDC_WANT_LIB_EXT1__
为整数常量。 1<stdio.h>
文件名 | - | 将文件流关联到的文件名 | |||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
模式 | - | 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. When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations. File access mode flag "x" can optionally be appended to "w" or "w+" specifiers. This flag forces the function to fail if the file exists, instead of overwriting it. (C11) When using fopen_s or freopen_s, file access permissions for any file created with "w" or "a" prevents other users from accessing it. File access mode flag "u" can optionally be prepended to any specifier that begins with "w" or "a", to enable the default fopen permissions. (C11) | 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. | When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations. | File access mode flag "x" can optionally be appended to "w" or "w+" specifiers. This flag forces the function to fail if the file exists, instead of overwriting it. (C11) | 当使用fopen_s或freopen_s时,用“w”或“a”创建的任何文件的文件访问权限会阻止其他用户访问它。文件访问模式标志“u”可以有选择地添加到以“w”或“a”开头的任何说明符中,以启用默认的fopen权限。(C11) |
文件访问模式字符串 | 含义 | 说明 | 行动如果文件已经存在 | 如果文件不存在,则采取行动 | |||||||||||||||||||||||||||||||||||||
“R” | 读 | 打开一个文件以供阅读 | 从开始阅读 | 未能打开 | |||||||||||||||||||||||||||||||||||||
“W” | 写 | 创建一个文件写入 | 破坏内容 | 创建新的 | |||||||||||||||||||||||||||||||||||||
“一个” | 附加 | 附加到文件 | 写入结束 | 创建新的 | |||||||||||||||||||||||||||||||||||||
“R +” | 阅读扩展 | 打开文件进行读取/写入 | 从开始阅读 | 错误 | |||||||||||||||||||||||||||||||||||||
“W +” | 写入扩展 | 创建一个用于读/写的文件 | 破坏内容 | 创建新的 | |||||||||||||||||||||||||||||||||||||
“A +” | 追加扩展 | 打开文件进行读取/写入 | 写入结束 | 创建新的 |
1)stream
成功的值的副本,失败时的空指针。
2)成功时为零(并且stream
写入的值的副本,*newstreamptr
错误时不为零(并且空指针被写入,*newstreamptr
除非newstreamptr
它本身是空指针)。
freopen
是通过I / O操作或通过I / O操作建立后,改变流的窄/宽方向的唯一方法fwide
。
以下代码重定向stdout
到一个文件。
#include <stdio.h>#include <stdlib.h> int main(void){ puts("stdout is printed to console"); if (freopen("redir.txt", "w", stdout) == NULL) { perror("freopen() failed"); return EXIT_FAILURE; } puts("stdout is redirected to a file"); // this is written to redir.txt fclose(stdout);}
输出:
stdout is printed to console
C11标准(ISO / IEC 9899:2011):
7.21.5.4 freopen函数(p:307)
K.3.5.2.2 freopen_s函数(p:590)
C99标准(ISO / IEC 9899:1999):
7.19.5.4 freopen函数(p:272-273)
C89 / C90标准(ISO / IEC 9899:1990):
4.9.5.4 freopen函数
fopenfopen_s(C11) | 打开一个文件(函数) |
---|---|
FCLOSE | 关闭文件(函数) |
| 用于freopen的C ++文档