【C语言】文件读写

访问文件

Programs may process no files, one file or several files. Each file used in a program must have a unique name and will have a different file pointer returned by fopen. All subsequent file processing functions after the file is opened must refer to the file with the appropriate file pointer. Files may be opened in one of several modes.

程序可以不处理任何文件,也可以处理一个文件或多个文件。程序中使用的每个文件都必须具有唯一的名称,并且将具有由 fopen 返回的不同文件指针。文件打开后的所有后续文件处理函数都必须使用适当的文件指针引用该文件。文件可以以多种模式之一打开。
FILE ∗   fopen ( const   char ∗  filename ,   const   char ∗  mode ) ; {\textit{FILE}}^{*}\ {\textbf{fopen}}{\left( {\textit{const char}}^{*}\ {\text{filename}},\ {\textit{const char}}^{*}\ {\text{mode}} \right)}; FILE fopen(const char filename, const char mode);
如果文件被成功打开,函数 fopen 返回一个指向 FILE 类型对象的指针。否则,返回一个空指针 NULLerrno 变量也会在失败时设置为系统特定的错误代码。

The program closes the file with fclose and terminates. Function fclose also receives the file pointer as an argument. If function fclose is not called explicitly the operating system normally will close the file when program execution terminates.

程序使用 fclose 关闭文件并终止。函数 fclose 还接收文件指针作为参数。如果函数 fclose 没有显式调用,操作系统通常会在程序执行终止时关闭文件。
int   fclose ( FILE*   stream ) ; {\textit{int }}{\textbf{fclose}}{\left({\textit{FILE* }}{\text{stream}}\right)}; int fclose(FILE* stream);
如果文件关闭成功返回数值 0 ,如果失败则返回数值 EOF
文件访问模式列表

Mode Description
r Open a file for reading.
w Create a file for writing. If the file already exists, discard the current contents.
a Append; open or create a file for writing at the end of the file.
r+ Open a file for update(reading and writing).
w+ Create a file for update. If the file already exists, discard the current contents.
a+ Append: open or create a file for update; writing is done at the end for the file.
rb Open a file for reading in binary mode.
wb Create a file for writing in binary mode. If the file already exists, discard the current contents.
ab Append; open or create a file for writing at the end of file in binary mode.
rb+ Open a file for update(reading and writing) in binary mode.
wb+ Create a file for update in binary mode. If the file already exists, discard the current contents.
ab+ Append: open or create a file for update in binary mode; writing is done at the end of the file.

用于输入各种计算机系统的 EOF (end-of-file)的组合键

Operating system Key combination
UNIX <return> or [Ctrl] + d
Windows [Ctrl] + z
Macintosh [Ctrl] + d

顺序访问文件

顺序访问的文件(Sequential-Access File)
int   fprintf ( FILE*   steam , const   char*   format , …   ) ; {\textit{int }}{\textbf{fprintf}}{\left({\textit{FILE* }}{\text{steam}},{\textit{const char* }}{\text{format}},\dots\right)}; int fprintf(FILE* steam,

相关推荐

  1. C语言文件

    2024-03-26 05:30:10       52 阅读
  2. 【计算机二级考试C语言C文件

    2024-03-26 05:30:10       49 阅读
  3. c++的文件

    2024-03-26 05:30:10       58 阅读
  4. C++的文件

    2024-03-26 05:30:10       69 阅读
  5. C++BMP文件

    2024-03-26 05:30:10       36 阅读
  6. C++二进制文件

    2024-03-26 05:30:10       32 阅读
  7. C# —— File文件

    2024-03-26 05:30:10       28 阅读
  8. C#】C#Excel文件

    2024-03-26 05:30:10       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 05:30:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 05:30:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 05:30:10       82 阅读
  4. Python语言-面向对象

    2024-03-26 05:30:10       91 阅读

热门阅读

  1. 如何运行vue项目

    2024-03-26 05:30:10       40 阅读
  2. WPF 界面变量绑定(通知界面变化)

    2024-03-26 05:30:10       38 阅读
  3. HTML5入门笔记

    2024-03-26 05:30:10       39 阅读
  4. VSCode基本配置(编辑器)

    2024-03-26 05:30:10       39 阅读
  5. CSRF,XSS攻击与预防

    2024-03-26 05:30:10       41 阅读
  6. numpy 学习笔记

    2024-03-26 05:30:10       43 阅读
  7. 数据结构奇妙旅程之深入解析快速排序

    2024-03-26 05:30:10       43 阅读
  8. Github 2024-03-25 开源项目日报Top10

    2024-03-26 05:30:10       44 阅读