当前位置:网站首页>【C语言进阶】文件操作(一)
【C语言进阶】文件操作(一)
2022-07-31 03:44:00 【沐曦希】
大家好我是沐曦希
文件操作
1.为什么使用文件
学习结构体时,写了通讯录的程序,当通讯录运行起来的时候,可以给通讯录中增加、删除数据,此时数据是存放在内存中,当程序退出的时候,通讯录中的数据自然就不存在了,等下次运行通讯录程序的时候,数据又得重新录入,如果使用这样的通讯录就很难受。
既然是通讯录就应该把信息记录下来,只有我们自己选择删除数据的时候,数据才不复存在。这就涉及到了数据持久化的问题,我们一般数据持久化的方法有,把数据存放在磁盘文件、存放到数据库等方式。
使用文件我们可以将数据直接存放在电脑的硬盘上,做到了数据的持久化。
2.什么是文件
磁盘上的文件是文件。
但是在程序设计中,我们一般谈的文件有两种:程序文件、数据文件(从文件功能的角度来分类的)。
2.1 程序文件
包括源程序文件(后缀为.c),目标文件(windows环境后缀为.obj),可执行程序(windows环境后缀为.exe)。
2.2 数据文件
文件的内容不一定是程序,而是程序运行时读写的数据,比如程序运行需要从中读取数据的文件,或者输出内容的文件。
以前所学到章节所处理数据的输入输出都是以终端为对象的,即从终端的键盘输入数据,运行结果显示到显示器上。
其实有时候我们会把信息输出到磁盘上,当需要的时候再从磁盘上把数据读取到内存中使用,这里处理的就是磁盘上文件。
2.3 文件名
一个文件要有一个唯一的文件标识,以便用户识别和引用。
文件名包含3部分:文件路径+文件名主干+文件后缀
例如: c:\code\test.txt
其中文件路径:c:\code
文件名主干:test
文件后缀:.exe
c:\code\test.txt也被称为绝对路径,而test.txt被称为相对路径。
为了方便起见,文件标识常被称为文件名。
3.文件的打开和关闭
fopen函数
FILE * fopen ( const char * filename, const char * mode );
fclose函数
int fclose ( FILE * stream );
#include<stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("test.txt");
return 1;
}
//读\写文件操作
//...
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
3.1 文件指针
缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。
每个被使用的文件都在内存中开辟了一个相应的文件信息区,用来存放文件的相关信息(如文件的名字,文件状态及文件当前的位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是有系统声明的,取名FILE.
例如:例如,VS2019编译环境提供的 stdio.h 头文件中有以下的文件类型申明:
typedef struct _iobuf
{
void* _Placeholder;
} FILE;
不同的C编译器的FILE类型包含的内容不完全相同,但是大同小异。
每当打开一个文件的时候,系统会根据文件的情况自动创建一个FILE结构的变量,并填充其中的信息,使用者不必关心细节。
一般都是通过一个FILE的指针来维护这个FILE结构的变量,这样使用起来更加方便。
可以创建一个FILE*的指针变量:
FILE* pf; //文件指针变量
定义pf是一个指向FILE类型数据的指针变量。可以使pf指向某个文件的文件信息区(是一个结构体变量)。通过该文件信息区中的信息就能够访问该文件。也就是说,通过文件指针变量能够找到与它关联的文件。
例如:
3.2 文件的打开和关闭
文件在读写之前应该先打开文件,在使用结束之后应该关闭文件。
在编写程序的时候,在打开文件的同时,都会返回一个FILE*的指针变量指向该文件,也相当于建立了指针和文件的关系。
ANSIC 规定使用fopen函数来打开文件,fclose来关闭文件。
//打开文件
FILE * fopen ( const char * filename, const char * mode );
//关闭文件
int fclose ( FILE * stream );
打开方式如下:
文件使用方式 | 含义 | 如果指定文件不存在 |
---|---|---|
“r”(只读) | 为了输入数据,打开一个已经存在的文本文件 | 出错 |
“w”(只写) | 为了输出数据,打开一个文本文件 | 建立一个新的文件 |
“a”(追加) | 向文本文件尾添加数据 | 建立一个新的文件 |
“rb”(只读) | 为了输入数据,打开一个二进制文件 | 出错 |
“wb”(只写) | 为了输出数据,打开一个二进制文件 | 建立一个新的文件 |
“ab”(追加) | 向一个二进制文件尾添加数据 | 出错 |
“r+”(读写) | 为了读和写,打开一个文本文件 | 出错 |
“w+”(读写) | 为了读和写,建议一个新的文件 | 建立一个新的文件 |
“a+”(读写) | 打开一个文件,在文件尾进行读写 | 建立一个新的文件 |
“rb+”(读写) | 为了读和写打开一个二进制文件 | 出错 |
“wb+”(读写) | 为了读和写,新建一个新的二进制文件 | 建立一个新的文件 |
“ab+”(读写) | 打开一个二进制文件,在文件尾进行读和写 | 建立一个新的文件 |
实例代码:
#include <stdio.h>
int main()
{
FILE* pFile;
//打开文件
pFile = fopen("myfile.txt", "w");
//文件操作
if (pFile != NULL)
{
fputs("fopen example", pFile);
//关闭文件
fclose(pFile);
}
return 0;
}
"w"写文件时候会把文件原来的内容销毁。
4.文件的顺序读写
4.1 文件函数
功能 | 函数名 | 适用于 |
---|---|---|
字符输入函数 | fgetc | 所有输入流 |
字符输出函数 | fputc | 所有输出流 |
文本行输入函数 | fgets | 所有输入流 |
文本行输出函数 | fputs | 所有输出流 |
格式化输入函数 | fscanf | 所有输入流 |
格式化输出函数 | fprintf | 所有输出流 |
二进制输入 | fread | 文件 |
二进制输出 | fwrite | 文件 |
4.1.1 fgetc函数
int fgetc ( FILE * stream );
#include<stdio.h>
int main()
{
FILE* pf = fopen("myfile.txt", "r");
if (pf == NULL)
{
perror("myfile.txt");
return 1;
}
//读文件
int ch = 0;
while ((ch = fgetc(pf)) != EOF)
{
printf("%c ", ch);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.1.2 fputc函数
int fputc ( int character, FILE * stream );
#include<stdio.h>
int main()
{
FILE* pf = fopen("myfile.txt", "w");
if (pf == NULL)
{
perror("myfile.txt");
return 1;
}
//写文件
fputc('a', pf);
char i = 0;
for (i = 'a'; i <= 'z'; i++)
{
fputc(i, pf);
}
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.1.3 fgets函数
char * fgets ( char * str, int num, FILE * stream );
#include<stdio.h>
int main()
{
FILE* pf = fopen("myfile.txt", "r");
if (pf == NULL)
{
perror("myfile.txt");
return 1;
}
//读文件
char arr[20];
fgets(arr, 5, pf);
printf("%s\n", arr);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.1.4 fputs函数
int fputs ( const char * str, FILE * stream );
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
FILE* pf = fopen("myfile.txt", "w");
if (pf == NULL)
{
perror("myfile.txt");
return 1;
}
struct S s = {
"zhangsan",25,50.5f };
//写文件
fprintf(pf, "%s %d %f", s.arr, s.age, s.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.1.6 fscanf函数
int fscanf ( FILE * stream, const char * format, ... );
#include<stdio.h>
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
FILE* pf = fopen("myfile.txt", "r");
if (pf == NULL)
{
perror("myfile.txt");
return 1;
}
struct S s = {
0 };
//读文件
fscanf(pf, "%s %d %f", s.arr, &(s.age), &(s.score));
printf("%s %d %f", s.arr, s.age, s.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
流:任何一个程序,只要运行起来就会默认打开3个流:
1.FILE* stdin——标准输入流(键盘)
2.FILE* stdout——标准输出流(屏幕)
3FILE* stderr——标准错误流(屏幕)
fprintf(stdout,"hello world\n");//在屏幕上打印hello world
4.1.7 fwrite函数
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
#include<stdio.h>
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
FILE* pf = fopen("myfile.txt", "w");
if (pf == NULL)
{
perror("myfile.txt");
return 1;
}
struct S s = {
"zhangsan",25,50.5f };
//读文件
fwrite(&s, sizeof(struct S), 1, pf);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}
4.1.8 fread函数
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
4.2 对比一组函数
scanf/fscanf/sscanf
printf/fprintf/sprintf
scanf是针对标准输入的格式化输入语句
printf是针对标准输出的格式化输出语句
fscanf是针对所有输入流的格式化输入语句
fprintf是针对所有输出流的格式化输出语句
sprintf是把一个格式化的数据转化成一个字符串,针对字符串
sscanf是把一个字符串转换成一个格式化数据,针对字符串
#include<stdio.h>
struct S
{
char arr[10];
int age;
float score;
};
int main()
{
struct S s = {
"zhangsan",25,50.5f };
char buf[100] = {
'\0' };
sprintf(buf, "%s %d %f", s.arr, s.age, s.score);
//把s中的格式化数据转化成字符串放到buf中
printf("%s\n", buf);//"zhangsan 25 50.500000"
struct S tmp = {
0 };
sscanf(buf, "%s %d %f", tmp.arr, &(tmp.age), &(tmp.score));
//从字符串中获取一个格式化的数据放到tmp中
printf("%s %d %f\n", tmp.arr, tmp.age, tmp.score);
return 0;
}
int sprintf ( char * str, const char * format, ... );
int sscanf ( const char * s, const char * format, ...);
第一个是字符串,第二个是格式化数据
5.写在最后
那么文件操作的上半部分就到这里。
边栏推荐
- els 方块向右移
- C primer plus study notes - 8, structure
- Postgresql 15 source code analysis (5) - pg_control
- Detailed explanation of TCP and UDP
- "DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction" paper notes
- STM32 problem collection
- 顺序表的实现
- ClickHouse: Setting up remote connections
- Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
- Thinking about data governance after Didi fines
猜你喜欢
(四)递归、可变参数、访问修饰符、理解 main 方法、代码块
VS QT - ui does not display newly added members (controls) || code is silent
Detailed explanation of TCP and UDP
Detailed explanation of TCP (2)
"A daily practice, happy water problem" 1331. Array serial number conversion
Key Technologies of Interface Testing
With 7 years of experience, how can functional test engineers improve their abilities step by step?
How Zotero removes auto-generated tags
浅识Flutter 基本组件之CheckBox组件
[Paper reading] Mastering the game of Go with deep neural networks and tree search
随机推荐
组件传值 provide/inject
SIP协议标准和实现机制
[shell basics] determine whether the directory is empty
Pytest电商项目实战(上)
Use of QML
浅识Flutter 基本组件之showDatePicker方法
(8) Math class, Arrays class, System class, Biglnteger and BigDecimal classes, date class
【AUTOSAR-RTE】-4-Port and Interface and Data Type
Component pass value provide/inject
5. How does the SAP ABAP OData service support the $filter operation
【SemiDrive源码分析】【MailBox核间通信】44 - 基于Mailbox IPCC RPC 实现核间通信(RTOS侧 IPCC_RPC Server 消息接收及回复 原理分析篇)
some of my own thoughts
Redis 使用LIST做最新评论缓存
Several common errors when using MP
Thinking about data governance after Didi fines
type_traits元编程库学习
The application and practice of mid-to-platform brand advertising platform
els block to the right
Key Technologies of Interface Testing
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea