文件读取/写入访问常量File Read/Write Access Constants

语法Syntax


#include <stdio.h>  

备注Remarks

这些常量指定为文件请求的访问类型(“a”、“r”或“w”)。These constants specify the access type ("a", "r", or "w") requested for the file. 可以使用访问类型来指定转换模式(“b”或“t”)和 模式(“c”或“n”)。Both the translation mode ("b" or "t") and the commit-to-disk mode ("c" or "n") can be specified with the type of access.

访问类型如下所述。The access types are described below.

“a”"a"
打开以便在文件末尾进行写入(追加);如果文件不存在,则首先创建它。Opens for writing at the end of the file (appending); creates the file first if it does not exist. 所有写入操作均在文件末尾发生。All write operations occur at the end of the file. 虽然使用 fseek 或 rewind 可重新定位文件指针,但在执行任何写入操作前,文件指针将始终被移回文件末尾。Although the file pointer can be repositioned using fseek or rewind, it is always moved back to the end of the file before any write operation is carried out.

“a+”"a+"
与如上所述相同,但还允许读取。Same as above, but also allows reading.

“r”"r"
打开以便读取。Opens for reading. 如果文件不存在或找不到,调用以打开文件操作将失败。If the file does not exist or cannot be found, the call to open the file will fail.

“r+”"r+"
打开以便读取和写入。Opens for both reading and writing. 如果文件不存在或找不到,调用以打开文件操作将失败。If the file does not exist or cannot be found, the call to open the file will fail.

“w”"w"
打开用于写入的空文件。Opens an empty file for writing. 如果给定文件存在,则其内容会被销毁。If the given file exists, its contents are destroyed.

“w+”"w+"
打开用于读取和写入的空文件。Opens an empty file for both reading and writing. 如果给定文件存在,则其内容会被销毁。If the given file exists, its contents are destroyed.

指定“r+”、“w+”或“a+”类型时,允许读取和写入(文件将处于打开状态以进行“更新”)。When the "r+", "w+", or "a+" type is specified, both reading and writing are allowed (the file is said to be open for "update"). 但是,在读取与写入之间切换时,必须有干预性的 fflushfsetposfseek 或 rewind 操作。However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. 可以为 fsetposfseek 操作指定当前位置。The current position can be specified for the fsetpos or fseek operation.

另请参阅See Also

_fdopen、_wfdopen _fdopen, _wfdopen
fopen、_wfopen fopen, _wfopen
freopen、_wfreopen freopen, _wfreopen
_fsopen、_wfsopen _fsopen, _wfsopen
_popen、_wpopen _popen, _wpopen
全局常量Global Constants