当前位置:网站首页>C语言的文件操作
C语言的文件操作
2022-07-28 05:26:00 【JuLiJuLi.】
前言:我们知道一个程序在运行时,数据是存储在内存中的,当退出程序后,数据也就销毁了,下次运行程序时又要重新输入数据,如果我们要达到程序退出时,数据依然存在,直到我们自己选择删除数据时数据才销毁,该如何做到呢?这就涉及到如何把数据写入电脑的硬盘中,以此来达到我们想要的效果,也达到了数据的持久化.
FILE*
首先我们要了解这个FILE指针,它是一个文件指针是用来创建一个FILE*的变量来访问一个文件的,一般我们通过这个文件指针来找到与它关联的文件.
如何打开和关闭一个文件呢,ANSIC 规定使用fopen函数来打开文件,用fclose函数来关闭文件.
## fopen函数原型
## fclose函数原型
当我们知道了用来打开文件的函数后还要知道打开文件的方式,下图将一一介绍:
#include<stdio.h>
int main()
{
FILE* pf; //创建一个文件指针类型变量
pf = fopen("li.txt", "w"); //打开文件,如果不存在则创建一个新的
if (pf == NULL) //判断fopen的返回值是否为空指针,失败会返回NULL指针
{
perror("fopen:");
return 1;
}
fclose(pf); //关闭文件
pf = NULL; //指针置为空
return 0;
}
这里程序运行后,此时我们的路径下就会创建出一个新的文件.
文件的顺序读写
文件的顺序读写函数以及功能:
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "w"); //以写的方式打开文件
if (pf == NULL)
{
perror("fopen:");
return 1;
}
char ch = 'a';
for (ch = 'a'; ch <= 'z'; ch++)
{
fputc(ch, pf); //给这个文件内录入信息字符'a'~'z'
}
fclose(pf); //关闭文件
pf = NULL;
return 0;
}
文件的随机读写
1.fseek函数原型
这个函数是根据文件指针的位置和对于起始位置的偏移量来定位文件指针.
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "r"); //因为我们这个文件中放的是字符'a'~'z'
if (pf == NULL)
{
perror("fopen:");
return 1;
}
int ch = fgetc(pf); //所以这里是a
printf("%c\n", ch);
fseek(pf, 3, SEEK_SET); //从起始位置向后偏移3,指向d
ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf); //上一次指针已经指向了d,向后走这次指向的是e
printf("%c\n", ch);
fclose(pf); //使用完关闭文件
pf = NULL;
return 0;
}
验证:
2.ftell函数原型
这个函数的作用是计算出此时这个文件指针对于起始位置的偏移量是多少,并返回它.
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "r"); //因为我们这个文件中放的是字符'a'~'z'
if (pf == NULL)
{
perror("fopen:");
return 1;
}
int ch = fgetc(pf); //所以这里是a
printf("%c\n", ch);
fseek(pf, 3, SEEK_SET); //从起始位置向后偏移3,指向d
ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf); //上一次指针已经指向了d,向后走这次指向的是e
printf("%c\n", ch);
long flag = ftell(pf); //计算偏移量
printf("对于起始位置的偏移量是:%ld\n", flag);//偏移量为5
fclose(pf); //使用完关闭文件
pf = NULL;
return 0;
}
验证:
3.rewind函数原型
这个函数的作用是让文件指针的位置回到起始位置
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "r"); //因为我们这个文件中放的是字符'a'~'z'
if (pf == NULL)
{
perror("fopen:");
return 1;
}
fseek(pf, 3, SEEK_SET); //从起始位置向后偏移3
long flag = ftell(pf); //计算偏移量
printf("对于起始位置的偏移量是:%ld\n", flag);
rewind(pf); //使用rewind函数使文件指针回到起始位置
flag = ftell(pf); //再计算一次偏移量大小
printf("对于起始位置的偏移量是:%ld\n", flag);//此时这里是0
fclose(pf); //使用完关闭文件
pf = NULL;
return 0;
}
验证:
边栏推荐
- 气传导蓝牙耳机哪个好、气传导蓝牙耳机排行榜
- 使用wampserver3.2.6时切换中文时造成启动失败
- 七夕送女朋友什么礼物好?不会送礼的男生速看!
- Pytorch learning note 4 - automatic calculation of gradient descent autograd
- Pyppeteer 被识别 绕过检测
- Getting started with hugging face
- Perl introductory learning (XI) file operation
- QT custom sliding button (beautiful and easy to use)
- Hugging face 的入门使用
- Esxi on arm 10/22 update
猜你喜欢

Talking about fluke optical cable certification? What is CFP? What is OFP?

2022年七夕礼物推荐!好看便宜又实用的礼物推荐

qt自定义滑动按钮(美观且使用方便)

量化交易机器人系统开发

Explain the installation of MSDN 2015 and precautions

七夕礼物送女生什么好?颜值在线又有心意的礼物推荐

clickhouse聚合之探索聚合内部机制

Word自动目录字体修改和行间距的问题

What about the insufficient memory of Clickhouse aggregation? Then improve the polymerization performance

Matlab simulation of radar imaging 4 - range resolution analysis
随机推荐
Monitor the CPU temperature of raspberry pie 4B installed with esxi on ARM
How to test industrial Ethernet cables (using fluke dsx-8000)?
Get the current directory in QT
JSON笔记
scrapy 命令
Matlab simulation of radar imaging 1 - LFM signal and its spectrum
Cautious speculation about fusion on Apple silicon
Systemmediasize startup option added in esxi 7.0 update 1C
Find the network address and broadcast address of the host according to the IP address and subnet mask
Solve the problem that the memory occupation is higher than that of the application process
Exploration of Clickhouse aggregation internal mechanism of aggregation
【学习笔记】线程创建
气传导蓝牙耳机哪个好、气传导蓝牙耳机排行榜
小程序自定义组件-数据,方法和属性
Machine learning note 5 - logistic regression
qt自定义滑动按钮(美观且使用方便)
qt绘画事件-设置背景图片
Fluke dtx-1800 and its accessories dtx-cha002 channel adapter channel replacement RJ45 socket notes
【学习笔记】编码能力
I heard that you are also practicing when I interviewed several junior interns.