C++ Reference: Standard C++ Library reference: C Library: cstdio: fopen

C++官网参考链接:https://cplusplus.com/reference/cstdio/fopen/

函数

fopen
FILE * fopen ( const char * filename, const char * mode );
打开文件
打开由形参filename指定的文件名的文件,并将其与一个流相关联,该流可在将来的操作中由返回的FILE指针识别。
流上允许的操作以及如何执行这些操作由mode形参定义。
如果知道返回的流没有引用交互设备,则返回的流在默认情况下是完全缓冲的(参见setbuf)。
返回的指针可以通过调用fclose或freopen从文件中解关联。所有打开的文件在正常程序终止时自动关闭。
运行环境至少支持同时打开FOPEN_MAX文件。

形参
filename
包含要打开的文件名的C字符串。
它的值应该遵循运行环境的文件名规范,并可以包括一个路径(如果系统支持)。
mode
包含文件访问模式的C字符串。它可以是:

"r"

read: Open file for input operations. The file must exist.

(读:打开文件进行输入操作。该文件必须存在。)

"w"

write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.

(写:为输出操作创建一个空文件。如果已经存在同名的文件,则丢弃其内容,并将该文件视为新的空文件。)

"a"

append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist.

(追加:打开文件以便在文件结束处输出。输出操作总是在文件结束处写入数据,并将其展开。重定位操作(fseek, fsetpos, rewind)将被忽略。如果文件不存在,则创建该文件。)

"r+"

read/update: Open a file for update (both for input and output). The file must exist.

(读/更新:打开一个文件进行更新(用于输入和输出)。该文件必须存在。)

"w+"

write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.

(写/更新:创建一个空文件并打开它进行更新(用于输入和输出)。如果已经存在具有相同名称的文件,则其内容将被丢弃,并将该文件视为新的空文件。)

"a+"

append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.

(追加/更新:打开一个文件进行更新(包括输入和输出),所有的输出操作都在文件结束处写入数据。重定位操作(fseek、fsetpos、rewind)影响下一个输入操作,但输出操作将位置移回文件结束处。如果文件不存在,则创建该文件。)

使用上面的模式说明符,文件将作为文本文件打开。为了将文件作为二进制文件打开,模式字符串中必须包含一个"b"字符。这个额外的“b”字符可以被附加在字符串的末尾(因此产生以下复合模式:"rb","wb","ab","r+b","w+b","a+b"),也可以插入到字母和复合模式("rb+","wb+","ab+")的"+"符号之间。
新的C标准(C2011,它不是C++的一部分)增加了一个新的标准子说明符("x"),它可以被附加到任何"w"说明符(形成"wx","wbx","w+x"或"w+bx"/"wb+x")。如果文件存在,此子说明符强制函数失败,而不是覆盖它。
如果附加字符按照顺序出现,则行为取决于库实现:一些实现可能会忽略附加字符,以便例如额外的"t"(有时用于显式地声明文本文件)被接受。
在某些库实现中,使用更新模式打开或创建文本文件可能会将stream视为二进制文件。
文本文件是包含文本行序列的文件。根据应用程序运行的环境,在文本模式下的输入/输出操作中可能会发生一些特殊字符转换,以使它们适应于特定于系统的文本文件格式。尽管在某些环境中不发生转换,文本文件和二进制文件的处理方式相同,但使用适当的模式可以提高可移植性。
对于打开进行更新的文件(包含"+"号的文件),允许对其进行输入和输出操作,在写入操作之后的读取操作之前,流应该被刷新(fflush)或重新定位(fseek、fsetpos、rewind)。在读取操作之后的写入操作(只要该操作没有到达文件结束)之前,流应该被重新定位(fseek,fsetpos,rewind)。

返回值
如果文件被成功打开,该函数返回一个指向FILE对象的指针,可用于在未来的操作中识别stream。
否则,返回空指针。
在大多数库实现中,errno变量在失败时也被设置为特定于系统的错误代码。

用例
/* fopen example */
#include
int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  return 0;

输出:

C++ Reference: Standard C++ Library reference: C Library: cstdio: fopen_第1张图片

C++ Reference: Standard C++ Library reference: C Library: cstdio: fopen_第2张图片

另请参考
fclose    Close file (function) (关闭文件(函数)) 
freopen    Reopen stream with different file or mode (function) (用不同的文件或模式重打开流(函数)) 
setbuf    Set stream buffer (function) (设置流缓冲区(函数)) 
setvbuf    Change stream buffering (function) (改变流缓冲(函数)) 
tmpfile    Open a temporary file (function) (打开一个临时文件(函数)) 
tmpnam    Generate temporary filename (function) (生成临时文件名(函数)) 

你可能感兴趣的:(C++,Reference,C,Library,c++,c语言,打开文件,fopen)