当前位置:网站首页>深度解析C语言文件操作以及常见问题
深度解析C语言文件操作以及常见问题
2022-07-29 12:57:00 【~小明学编程】
作者:~小明学编程
文章专栏:C语言基础知识
目之所及皆为回忆,心之所向皆为过往
目录
为什么我们要使用文件
在我们写程序的时候,大家一定发现了一个问题就是我们的程序在运行完了之后就啥也没有了就如我们的通讯录小程序我们在运行程序的时候会对其输入很多的人物信息然后我们可以对其进行修改查询等操作,但是当我们结束该程序的时候我们所输入的数据会随着我们的程序的结束而被程序给释放掉,那么我们如何解决该问题呢?这时我们会想到要是我们能有一个文件存储这些信息,当我们下次再次打开该程序的时候接着读取上一次的信息就好了,这便是我们本次要介绍的内容,我们通过一些函数直接操作文件的读写。
文件的打开和关闭
文件指针
FILE* pf;//文件指针变量文件的打开和关闭
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");//打开文件
fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
fclose(pf);//关闭文件
pf = NULL;
return 0;
}文件的打开方式

文件的顺序读写

fgetc

函数的功能主要就是从标准流中读取一个字符然后写入文件中
int main()
{
FILE* pw = fopen("test.txt", "w");
if (pw == NULL)
{
printf("%s\n", strerror(errno));
}
fputc('h', pw);
fputc('a', pw);
fputc('h', pw);
fputc('a', pw);
fclose(pw);
pw = NULL;
printf("读取完毕!\n");
return 0;
}
fputc

int main()
{
FILE* pw = fopen("test.txt", "r");
if (pw == NULL)
{
printf("%s\n", strerror(errno));
}
printf("%c", fgetc(pw));
printf("%c", fgetc(pw));
printf("%c", fgetc(pw));
printf("%c", fgetc(pw));
fclose(pw);
pw = NULL;
printf("读取完毕!");
return 0;
}通过fgetc函数我们将刚刚读入的字符再次的读出来。

fputs

函数的功能主要是写入一个字符串进入我们的文件之中,其中有两个参数,一个是我们的字符串地址,还有一个是文件指针
int main()
{
char str[10]={0};
FILE* pw = fopen("test.txt", "w");
if (pw == NULL)
{
printf("%s\n", strerror(errno));
}
//fgets(str, 10, pw);
//printf("%s", str);
//fgets(str, 10, pw);
//printf("%s", str);
fputs("hello\n", pw);
fputs("world", pw);
fclose(pw);
pw = NULL;
return 0;
}
fgets

函数的主要功能就是 从我们的流也就是文件中读取字符串,我们可以看到该函数一共有三个参数,分别是把我们读取的字符串要放入的地址,读取的个数,以及我们的文件指针。
int main()
{
char str[10]={0};
FILE* pw = fopen("test.txt", "r");
if (pw == NULL)
{
printf("%s\n", strerror(errno));
}
fgets(str, 10, pw);
printf("%s", str);
fgets(str, 10, pw);
printf("%s", str);
//fputs("hello\n", pw);
//fputs("world", pw);
fclose(pw);
pw = NULL;
return 0;
}
fscanf

函数的主要功能就是格式化的从文件中输入想要的数据
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { 0 };
FILE* p = fopen("text.txt", "r");
if (p == NULL)
{
printf("%s", strerror(errno));
}
if (p == 0)
{
return 1;
}
fscanf(p, "%s %d %f", s.name, &(s.age), &(s.score));
printf("%s %d %f", s.name, s.age, s.score);
fclose(p);
p == NULL;
return 0;
}
fprintf

函数的主要功能就是格式化的将数据输出到文件之中
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { "张三",20,100.0 };
FILE* p = fopen("text.txt", "w");
if (p == NULL)
{
printf("%s", strerror(errno));
}
if (p == 0)
{
return 1;
}
fprintf(p,"%s %d %f", s.name, s.age, s.score);
fclose(p);
p == NULL;
return 0;
}
fwrite

函数主要功能是以二进制的形式写入文件数据
其中的参数分别是要写入的项目地址,字节的大小,要写入的最大项目数和文件指针
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { "张三",20,60 };
FILE* pf = fopen("test.txt", "wb");
if (pf == NULL)
{
return 1;
}
fwrite(&s, sizeof(struct S), 1, pf);
return 0;
}
fread

函数的主要功能是从流中以二进制形式读入数据,参数和fwrite一样。
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { 0 };
FILE* pf = fopen("test.txt", "rb");
if (pf == NULL)
{
return 1;
}
fread(&s, sizeof(struct S), 1, pf);
printf("%s %d %.1lf", s.name, s.age, s.score);
return 0;
}
文件的随机读取
fseek
该函数可以让我们从指定的位置读取文件的内容,传参以此是文件指针,偏移量和读取的位置
int main()
{
FILE* p = fopen("test.txt", "r");
if (p == NULL)
{
return 1;
}
fseek(p, 2, SEEK_CUR);
printf("%c", fgetc(p));
fclose(p);
p = NULL;
return 0;
}我们可以看到我们的偏移量是2,读取位置是当前文件指针的位置

ftell
函数的主要作用是返回一个文件指针相对于起始位置的偏移量
int main()
{
FILE* p = fopen("test.txt", "r");
if (p == NULL)
{
return 1;
}
fseek(p, 2, SEEK_CUR);
printf("%c\n", fgetc(p));
int ret = ftell(p);
printf("%d", ret);
fclose(p);
p = NULL;
return 0;
}
我们可以看到返回的是3。
rewind
此函数的目的是让文件指针重新回到起始的位置。
文件读取结束的判定
大怨种feof
对feof函数最大的误解就是觉得它是用来判断判断文件是否结束的,然而它的作用并不是如此,用它的时候文件一定是读取结束只是它可以告诉我们文件读取结束是因为什么是文件读完了还是文件读取失败了。
1. 文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者 NULL ( fgets )例如:fgetc 判断是否为 EOF .fgets 判断返回值是否为 NULL .2. 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。例如:fread判断返回值是否小于实际要读的个数。
int main()
{
int c; // 注意:int,非char,要求处理EOF
FILE* fp = fopen("test.txt", "r");
if (!fp) {
perror("File opening failed");
return EXIT_FAILURE;
}
//fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
{
putchar(c);
}
//判断是什么原因结束的
if (ferror(fp))
puts("I/O error when reading");
else if (feof(fp))
puts("End of file reached successfully");
fclose(fp);
return 0;
}
边栏推荐
猜你喜欢

MySQL常用的日期时间函数
![[WeChat applet] One article to solve button, input, image components](/img/a4/56df6c530c41f6cff865053f9f8f2c.png)
[WeChat applet] One article to solve button, input, image components

Leetcode67. 二进制求和

轻松学Pytorch-Pytorch可视化

Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)

How to set the explosion rate of legendary humanoid?Humanoid increase tutorial

Leetcode65. 有效数字

Bika LIMS - SENAITE using open source LIMS set (users, roles and departments)

Scala 简介一

万字长文,揭秘华为数据治理体系!
随机推荐
DVWA full level customs clearance tutorial
MLX90640 infrared thermal imaging temperature measuring sensor module development notes (9)
mysql根据多字段分组——group by带两个或多个参数
The whole process of installing Oracle database on CentOS7
浅谈MES系统质量管理的方案
pycharm专业版使用
MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
关于ESI研究前沿的思考和使用方法研究
C language game ------ greedy snake ---- for Xiaobai
JS_ deleting the invalid data in the array undefined '0' null false NaN
【MySQL视图】视图的概念、创建、查看、删除和修改
Dataset:FIFA 2018 Statistics数据集(Predict FIFA 2018 Man of the Match预测2018年国际足联最佳球员)的简介、下载、使用方法之详细攻略
Mysql stored procedures, rounding
SIP系统组成格式
来自 Qt 官网的呐喊
JUC阻塞队列-ArrayBlockingQueue
电子游戏的核心原理
[Numpy] np.where
MySql string splitting realizes the split function (field splitting, column switching, row switching)
Sql file import database - nanny level tutorial

