当前位置:网站首页>【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、文件缓冲区
文件在储存的时候,会先将数据储存在缓冲区中,缓冲区存满之后,一次性写入文件中
文件读出数据时也是一样,会先将数据储存在缓冲区中,缓冲区存满之后,一次性读出来
只要你关闭文件,就会刷新缓冲区
边栏推荐
- Analysis on the specific execution process of an insert statement in MySQL 8.0 (3)
- Is it safe to open a securities account? Is it reliable?
- Design of intelligent test paper generation system
- 区域工业互联网市场成绩单,百度智能云开物第二
- 如何快速完成磁盤分區
- Common usage of LINQ in C #
- 《CLR via C#》读书笔记-CLR寄宿与AppDomain
- C#使用WinExec调用exe程序
- C语言库函数--strstr()
- September 21, 2020 referer string segmentation boost gateway code organization level
猜你喜欢

Common usage of LINQ in C #

全面理解Volatile关键字

2020-10-17:刷题1

AQS之ReentrantLock源码解析

Slide the custom control to close the activity control

《MongoDB入门教程》第02篇 MongoDB安装

查看CSDN的博客排名

《CLR via C#》读书笔记-加载与AppDomain

Reading notes of CLR via C -clr boarding and AppDomain

Alibaba cloud server is installed and configured with redis. Remote access is unavailable
随机推荐
mysql 8.0 一条insert语句的具体执行流程分析(二)
攻防世界-Re-insfsay
BUUCTF--reverse1
《MongoDB入门教程》第02篇 MongoDB安装
Recyclerview sticky (suspended) head
The difference between & & and &
Application of Pgp in encryption technology
C#MDI打开子窗体去掉自动生成的菜单栏
If [not] exists in MySQL
ssh密钥泄露(B模块赛题)——应用服务漏洞扫描与利用
2020-09-18 referer authentication URL escape
DevExpress的双击获取单元格数据
UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. 笔记
The product strength is not inferior to that of BYD. Geely Dihao l Raytheon hi · x delivered 10000 units in the first month
Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX
通过Win32API调用另一界面的按钮
WinForm uses zxing to generate QR code
InnoDB in MySQL_ page_ Cleaners details
Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
AQS之Atomic详解