当前位置:网站首页>[advanced C language] file operation (II)
[advanced C language] file operation (II)
2022-06-30 00:11:00 【Haozai lives today】
List of articles
1、 Random reading and writing of files
1.1、fseek function
fseek Function function , Locate the file pointer according to its position and offset
Random read file
#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));
}
// Reading documents
int ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
// Locate file pointer
//fseek(pf, 1, SEEK_CUR);// Three parameters ( flow 、 Offset 、 File pointer location )
//fseek(pf, 3, SEEK_SET);
fseek(pf, -1, SEEK_END);
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
Write files randomly
#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));
}
// Writing documents
int ch = 0;
for (ch = 'a'; ch <= 'z'; ch++)
{
fputc(ch, pf);
}
// Locate file pointer
fseek(pf, -1, SEEK_END);// You can modify what you write in the specified location
fputc('#', pf);
fclose(pf);
pf = NULL;
return 0;
}
1.2、ftell function
ftell The offset() function tells you the offset of the current file pointer ( Tell you the current file pointer position )
#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));
}
// Reading documents
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 function
rewind The file pointer offset() function sets the file pointer offset to 0, The file pointer points back to the beginning of the file
#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));
}
// Reading documents
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、 Judge whether the file reading is over
1、 Whether the reading of text file is finished
Determine whether the return value is EOF(fgetc) perhaps NULL(fgets)
for example :
fgetc Judge whether it is EOF;
fgets Determine whether the return value is NULL;
2、 Binary file reading end judgment , Judge whether the return value is less than the actual number to be read
for example :
fread Judge whether the return value is less than the actual number to be read
3、 File buffer
When documents are stored , The data will be stored in the buffer first , After the buffer is full , Write to file at one time
The same is true when reading data from a file , The data will be stored in the buffer first , After the buffer is full , Read it at one time
As long as you close the file , Will flush the buffer
边栏推荐
- 基于zfoo开发项目的一些规范
- Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
- Summarize Flink runtime architecture in simple terms
- Embedded development: Hardware in the loop testing
- 【微信小程序】认识小程序项目的基本组成结构
- The role of VMware virtual machine
- Virtual machine online migration based on openstack
- JVM之栈空间
- HTAP x cloud native: tidb accelerates the release of data value and realizes data agility
- QT learning 03 birth and essence of QT
猜你喜欢

How to write controller layer code gracefully?

What is IGMP? What is the difference between IGMP and ICMP?

Summarize Flink runtime architecture in simple terms

Fine grained identification, classification, retrieval data set collation

This PMP Exam (June 25), some people are happy and others are worried. That's why

Leetcode (680) -- verifying palindrome string II

JS的初步语法

Root cause of glideexception: failed decodepath{directbytebuffer- > gifdrawable- > drawable}
![[advanced C language] address book implementation](/img/e6/8a51d519d31ec323cf04c59a556325.png)
[advanced C language] address book implementation

AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
随机推荐
QT learning 06 widgets and window types
Mysql:sql overview and database system introduction | dark horse programmer
Stack space of JVM
项目一:部署 LAMP ecshop电商平台
Solr basic operation 11
HTAP x cloud native: tidb accelerates the release of data value and realizes data agility
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
MySQL functions and constraints
What is IGMP? What is the difference between IGMP and ICMP?
爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
modelsim的TCL脚本的define incdir命令解析
QT learning 01 GUI program principle analysis
FPGA Development (2) -- IIC communication
Teach you step by step to install webwind notes on edge browser
Leetcode (76) -- Minimum Covering substring
Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
Halcon practical: design idea of solder joint detection
Exploration and Practice on the future direction of byte cloud database
JS draw polar color gradient
Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)