当前位置:网站首页>【C语言进阶】文件操作(二)
【C语言进阶】文件操作(二)
2022-06-29 09:36:00 【皓仔活在今天】
1、文件随机读写
1.1、fseek函数
fseek函数的作用,根据文件指针的位置和偏移量来定位文件指针
随机读文件
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
FILE* pf = fopen("text.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
//读文件
int ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
//定位文件指针
//fseek(pf, 1, SEEK_CUR);//三个参数(流、偏移量、文件指针位置)
//fseek(pf, 3, SEEK_SET);
fseek(pf, -1, SEEK_END);
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
随机写文件
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
FILE* pf = fopen("text.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
//写文件
int ch = 0;
for (ch = 'a'; ch <= 'z'; ch++)
{
fputc(ch, pf);
}
//定位文件指针
fseek(pf, -1, SEEK_END);//可以在指定位置修改你写入的内容
fputc('#', pf);
fclose(pf);
pf = NULL;
return 0;
}
1.2、ftell函数
ftell函数的作用是告诉你当前文件指针的偏移量(告诉你当前文件指针位置)
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
FILE* pf = fopen("text.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
//读文件
int ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
int ret = ftell(pf);
printf("%d\n", ret);
fclose(pf);
pf = NULL;
return 0;
}
1.3、rewind函数
rewind函数的作用是把文件指针偏移量置为 0,文件指针重新指向文件开头
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
FILE* pf = fopen("text.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
//读文件
int ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
int ret = ftell(pf);
printf("%d\n", ret);
rewind(pf);
ret = ftell(pf);
printf("%d\n", ret);
fclose(pf);
pf = NULL;
return 0;
}
2、判断文件读取是否结束
1、文本文件读取是否结束
判断返回值是否为EOF(fgetc)或者NULL(fgets)
例如:
fgetc判断是否为EOF;
fgets判断返回值是否为NULL;
2、二进制文件读取结束判断,判断返回值是否小于实际要读取的个数
例如:
fread判断返回值是否小于实际要读的个数
3、文件缓冲区
文件在储存的时候,会先将数据储存在缓冲区中,缓冲区存满之后,一次性写入文件中
文件读出数据时也是一样,会先将数据储存在缓冲区中,缓冲区存满之后,一次性读出来
只要你关闭文件,就会刷新缓冲区
边栏推荐
- Arc view and arc viewpager
- 《MongoDB入门教程》第02篇 MongoDB安装
- Dev使用过程中的基本操作
- Learn spark computing framework in practice (01)
- Is it safe to open a stock account with the QR code given by the manager of a securities firm? I want to open an account
- IIS server related error
- The product strength is not inferior to that of BYD. Geely Dihao l Raytheon hi · x delivered 10000 units in the first month
- Fully understand the MESI cache consistency protocol
- 如何优雅的写 Controller 层代码?
- How to quickly complete disk partitioning
猜你喜欢

共2600页!又一份神级的面试手册面世~

Real time value transfer from C form to another form

Dev使用过程中的基本操作

Hystrix fuse: Service fusing and service degradation

全面理解MESI缓存一致性协议

BUUCTF RE-easyre

Arc view and arc viewpager

Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.

Ora-01950 does not have permission on tablespace

Linux下Redis安装及集群搭建
随机推荐
Is it safe to open a stock account with the QR code given by the manager of a securities firm? I want to open an account
30岁,女,普通软件测试媛,对职业的迷茫和焦虑
SQL Server 数据库的连接查询
在实践中学习Spark计算框架(01)
Reading notes of CLR via C -clr boarding and AppDomain
Learn spark computing framework in practice (00)
2020-09-21 visual studio header file and Library Directory configuration
Graphics learned from jigsaw puzzles h
《CLR via C#》读书笔记-加载与AppDomain
BUUCTF RE-easyre
Dev使用过程中的基本操作
std::unique_ PTR < T> and boost:: scoped_ Particularity of PTR < t >
BUUCTF--新年快乐
If [not] exists in MySQL
Arc view and arc viewpager
Contents of advanced mathematics
Analysis of reentrantlock source code of AQS
Comment terminer rapidement une partition de disque
云主机端口扫描
BUUCTF--reverse1