首页 > 其他分享 >【C语言】文件读写

【C语言】文件读写

时间:2024-03-21 23:01:17浏览次数:28  
标签:文件 const file 读写 fclose C语言 mode FILE

Files & Streams

访问文件

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,

标签:文件,const,file,读写,fclose,C语言,mode,FILE
From: https://blog.csdn.net/m0_59577534/article/details/136774292

相关文章

  • C语言解决水仙花问题
    题目叙述:水仙花数是 指一个三位数,它的每个位上的数字的3次幂之和等于它本身。(例如:153=13+53+35153=13+53+35)找到所有的水仙花数并按行打印,按从小到大的顺序输出。思路:首先确定范围,三位数(100--999),其次确定百位、十位、个位要怎么表示,令一个位数为i百位(a)a=i/100  ......
  • C语言解决切面条问题
    题目叙述:一根高筋拉面,中间切一刀,可以得到2根面条。如果先对折1次,中间切一刀,可以得到3根面条。如果连续对折2次,中间切一刀,可以得到5根面条。那么,连续对折10次,中间切一刀,会得到多少面条呢?分析:可以拿一张纸撕1条,对折3次从中间撕开是9块1次------3---2次------5---3次----......
  • C语言实现反转整数
    题目描述:从标准输入流(控制台)中获取一个整数 num,将其 按位反转 后通过输出语句输出反转后的整数,保留原来整数的正负性。思路:前提是num不等于0首先我们需要定义一个中间变量 temp 来存放当前 num 的最小位,获取最小位存在temp---temp=num%10通过 result=result*1......
  • c语言的特点
    C语言的特点可谓既鲜明又多样,下面列举几个主要的方面:简洁紧凑、灵活方便:C语言只有32个关键字,9种控制语句,程序书写形式自由,区分大小写。把高级语言的基本结构和语句与低级语言的实用性结合起来。C语言可以像汇编语言一样对位、字节和地址进行操作,而这一切又是在高级语言编译系......
  • 字幕文件vtt转srt
    importrecontent=[]srt=[]withopen('input.vtt','r')asopen_file: forlinesinopen_file: lines=lines.replace('WEBVTT','')#删除WEBVTT#vtt文件中时间可以是00:00.000#srt文件中时间必须是00:00:00,000 ......
  • cad vba 打开excel并弹窗打开指定文件
     CADvba代码实现打开excel,并通过对话框选择xls文件,并打开此文件进行下一步操作。代码如下:OptionExplicit#IfVBA7ThenPrivateDeclarePtrSafeFunctionts_apiGetOpenFileNameLib"comdlg32.dll"Alias"GetOpenFileNameA"(tsFNAstsFileName)AsBooleanPriva......
  • 冒泡排序和选择排序--C语言
    冒泡排序(升序):设计思想:每两个相邻的数进行比较,大的往后走详细过程:例:99,100,88,80,100,90,77,22,33,90第一遍:99与100比较,100大,继续向后走,100与88比较,100大,100与88交换一下位置,继续向后走100与80比较,100大,100与80交换一下位置,继续向后走100与100比较,相等,不需要......
  • C语言中的printf和sprintf的用法及区别
    sprintf函数是C语言中用于格式化输出到字符串的函数。它的原型如下:intsprintf(char*buffer,constchar*format,[argument]…);str:指向存储输出结果的字符数组的指针。format:格式化字符串,包含要输出的文本和格式说明符。[argument]:可变参数列表,用于提供要插入格式化......
  • C语言常用格式字符
    %d或%i  有符号十进制整数%u  无符号十进制整数%ld  有符号长整型%c  字符%s  字符的字符串%f  十进制浮点数//------------------------分隔符------------------------%o  有符号八进制%x  无符号十六进制整数//--------------------......
  • 【C语言】模拟实现 atoi
    文章目录atoi()函数模拟实现思路分析代码呈现atoi()函数通过上述cplusplus和MSDN对atoi函数的介绍我们可以得出以下几个关键点库函数:<stdlib.h>形参:constchar*str返回值:int作用:atoi函数是将一个字符串转化成一个整型并忽视字符串中的字符举个例子/*atoi......