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

函数
<cstdio>
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 <stdio.h>
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. Conditional project or library reference in Visual Studio

    Conditional project or library reference in Visual Studio In case you were wondering why you haven't ...

  2. link library 、target library、symbol library、synthetic library对照分析-基础小知识(九)

    文章目录 1.1 概念介绍 1.2 疑问解析 参考文档 DC 应用过程中涉及link library .target library.symbol library.synthetic library, ...

  3. undefined reference to `__stack_chk_guard' .. undefined reference to `__stack_chk_fail'

    1. 编译出错 undefined reference to `__stack_chk_guard' undefined reference to `__stack_chk_fail' 解决方法-1: ...

  4. git fatal: cannot lock ref ‘HEAD‘:unable to resolve reference‘refs/heads/main‘:reference broken

    问题: push的时候,电脑强制关机再启动,vscode中所有文件都变成了绿色,暂存待提交状态 当我再次提交时,就出现错误:git fatal: cannot lock ref 'HEAD':unab ...

  5. undefined reference to 'floor'/undefined reference to

    undefined reference to 'floor'/undefined reference to `& undefined reference to 'pow'/undefined ...

  6. 问题排查--@cannot lock ref 'HEAD': unable to resolve reference 'refs/heads/master': reference broken解决方法

    @cannot lock ref 'HEAD': unable to resolve reference 'refs/heads/master': reference broken解决 作者用的是ID ...

  7. C++ Reference: Standard C++ Library reference: C Library: cmath: cbrt

    C++官网参考链接:https://cplusplus.com/reference/cmath/cbrt/ 函数  <cmath> <ctgmath> cbrt C99 dou ...

  8. C++ Reference: Standard C++ Library reference: C Library: cmath: erf

    C++官网参考链接:https://cplusplus.com/reference/cmath/erf/ 函数  <cmath> <ctgmath> erf C99 doubl ...

  9. C++ Reference: Standard C++ Library reference: C Library: cstdio: printf

    C++官网参考链接:https://cplusplus.com/reference/cstdio/printf/ 函数  <cstdio> printf int printf ( cons ...

最新文章

  1. #Java夜未眠# 读书笔记
  2. asp.net 无法获取客户端请求的真实协议https
  3. python实现二分查找算法_python实现二分查找算法
  4. 电动葫芦使用注意事项(转载)
  5. 两个大屏可视化案例的布局与实现
  6. 外边距合并(HTML、CSS)
  7. 【干货】最新app源码下载:200款优秀Android项目源码
  8. Arduino UNO测量电容值
  9. xp计算机调亮度,老xp系统怎么调亮度台式电脑(教你XP系统如何调节亮度)
  10. 北京城市总体总体规划 下载_总体表现
  11. java 问号运算符_JAVA问号?运算符的用法,问号表达式
  12. 初识Calcite——使用实例
  13. 前端性能指标:白屏和首屏时间的计算
  14. MySQL 03 高级查询(一)
  15. webRTC基础入门
  16. 详解两类AI芯片架构和关键技术
  17. Flask 之父:我不觉得有异步压力
  18. Python保姆级教程.pdf,太全了!
  19. 面对CUDA报错的种种解决办法
  20. 天津理工计算机学院调剂,2019年硕士研究生调剂通知

热门文章

  1. Bootstrap浏览器兼容性
  2. HTTPS/HTTP网络代理
  3. 封装bootstrap-treegrid组件
  4. 随心所欲大小写转换自定义函数
  5. Word中将多个表格内容批量居中:通过“宏”操作
  6. 3DMAX打开模型一直未响应
  7. 学习reflux的总结
  8. JOOQ学习笔记:分页、排序、字段重命名的写法
  9. xadmin里面的设置
  10. 电机控制器,FPGA 硬件电流环 基于FPGA的永磁同步伺服控制系统的设计