当前位置:网站首页>Detailed explanation of file operation (2)
Detailed explanation of file operation (2)
2022-04-23 16:32:00 【The mountain stream is clear and blue】
Catalog
3. Determination of the end of file reading
1. Random reading of files
1.1 fseek
The default reading of files starts from the first element
for example :
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* pf = fopen("test.txt", "r");// In the way of reading
if (pf == NULL)
{
perror("fopen");
return 1;
}
char ch = fgetc(pf);// Read a character and put it in ch in , After reading, the pointing position will be shifted backward
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
test.txt The contents of the document :

Running results :
![]()

The first parameter is the file pointer , The second parameter is the offset , The initial position of the third parameter ( as follows )
SEEK_CUR// The current location of the file pointer
SEEK_END// The position at the end of the file
SEEK_SET// Where the file starts
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* pf = fopen("test.txt", "r");// In the way of reading
if (pf == NULL)
{
perror("fopen");
return 1;
}
char ch = fgetc(pf);// Read a character and put it in ch in
printf("%c\n", ch);
ch = fgetc(pf);
printf("%c\n", ch);
fseek(pf, 2, SEEK_CUR);// from c Start to shift back 2 Characters
ch = fgetc(pf);
printf("%c\n", ch);
fseek(pf, 0, SEEK_END);// The character of the current position is '\0'
ch=fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
Running results :


#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* pf = fopen("test.txt", "w");// In writing
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputc('a', pf);
fputc('b', pf);
fputc('c', pf);
fputc('d', pf);
fseek(pf, -3, SEEK_CUR);// The current position is offset forward 3 A place
fputc('e', pf);
fclose(pf);
pf = NULL;
return 0;
}
Running results :

1.2 ftell

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* pf = fopen("test.txt", "w");// In writing
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputc('a', pf);
fputc('b', pf);
fputc('c', pf);
fputc('d', pf);
fseek(pf, -3, SEEK_CUR);// The current position is offset forward 3 A place
fputc('e', pf);
long pos = ftell(pf);// The return type of this function is long type
printf("%ld\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
Running results :
![]()
1.3 rewind
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE* pf = fopen("test.txt", "w");// In the way of reading
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputc('a', pf);
fputc('b', pf);
fputc('c', pf);
fputc('d', pf);
fseek(pf, -3, SEEK_CUR);// The current position is offset forward 3 A place
fputc('e', pf);
long pos = ftell(pf);
printf("%ld\n", pos);
rewind(pf);// Return to the starting position
pos = ftell(pf);
printf("%ld\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
Running results :
![]()
2. Text files and binaries
According to the organization of data , Data files are called text file perhaps Binary .The data is stored in memory as Binary system Form storage of , If the output without conversion is to external memory , Namely Binary .If it's required to use ASCII code Form storage of , You need to convert before storing . With ASCII The file stored in the form of characters is writing This document
#include <stdio.h>
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");
fwrite(&a, 4, 1, pf);// The binary form is written to the file
fclose(pf);
pf = NULL;
return 0;
}
The contents of this file are binary files

Add to source file

Right click to select the opening method , With Binary editor Open up

It's about

because VS2019 With Small end storage So the result is 10 27 00 00
3. Determination of the end of file reading
During file reading , You can't use feof The return value of the function is directly used to determine whether the end of the file .
feof function Apply when the file reading is finished , The judgment is that the read failed and ended , Or end of file .
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. Judgment of reading end of binary file , Judge whether the return value is less than the actual number to be read .for example :fread Determine the return value Whether it is less than the actual number to be read .
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int c; // Be careful :int, Not char, Ask to deal with EOF
FILE* fp = fopen("test.txt", "r");
if (!fp) {
perror("File opening failed");
return EXIT_FAILURE;
}
//fgetc When reading fails or the end of the file is encountered , Will return to EOF
while ((c = fgetc(fp)) != EOF) // standard C I/O Read file cycle
{
putchar(c);
}
putchar('\n');
// Judge why it ended
if (ferror(fp))
puts("I/O error when reading");
else if (feof(fp))
puts("End of file reached successfully");
fclose(fp);
}
Running results :

4. File buffer
ANSIC The standard is “ Buffer file system ” Processing of data files , The so-called buffer file system means that the system automatically opens up a block in memory for each file being used in the program “ File buffer ” . Data output from memory to disk is first sent to a buffer in memory , pack full The buffer is then sent to disk together . If you read data from disk to computer , Then read the data from the disk file and input it into the memory buffer ( Full buffer ), And then send the data from the buffer to the program data area one by one ( Program variables, etc ). The size of the buffer depends on C The compiler system decides .

for example :
#include <stdio.h>
#include <windows.h>
int main()
{
FILE* pf = fopen("test.txt", "w");
fputs("abcdef", pf);// Put the code in the output buffer first
printf(" sleep 10 second - The data has been written , open test.txt file , Found no content in the file \n");
Sleep(10000);
printf(" Refresh buffer \n");
fflush(pf);// When the buffer is flushed , Write the data in the output buffer to a file ( disk )
// notes :fflush In high version VS It can't be used on
printf(" Sleep again 10 second - here , Open again test.txt file , There's something in the file \n");
Sleep(10000);
fclose(pf);
// notes :fclose When closing a file , It also flushes the buffer
pf = NULL;
return 0;
}
Running results :


10 Seconds later


Conclusion :Because there is a buffer , C Language when operating files , Need to do Refresh buffer perhaps Close the file at the end of the file operation .If you don't do , May cause problems in reading and writing files .
版权声明
本文为[The mountain stream is clear and blue]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231630298829.html
边栏推荐
- 深度学习100例 | 第41天-卷积神经网络(CNN):UrbanSound8K音频分类(语音识别)
- How to upgrade openstack across versions
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
- 力扣-198.打家劫舍
- The system research problem that has plagued for many years has automatic collection tools, which are open source and free
- Day (4) of picking up matlab
- Day (9) of picking up matlab
- Sail soft calls the method of dynamic parameter transfer and sets parameters in the title
- 文件系统读写性能测试实战
- There is a problem with the light switch from 1 to 100
猜你喜欢

Day (6) of picking up matlab

Use if else to judge in sail software - use the title condition to judge

捡起MATLAB的第(6)天
JIRA screenshot

Postman batch production body information (realize batch modification of data)

NVIDIA显卡驱动报错

Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1

Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot

文件操作详解(2)

The first line and the last two lines are frozen when paging
随机推荐
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Change the icon size of PLSQL toolbar
Database dbvisualizer Pro reported file error, resulting in data connection failure
力扣-198.打家劫舍
The first line and the last two lines are frozen when paging
ESXi封装网卡驱动
Differences between MySQL BTREE index and hash index
Detailed explanation of gzip and gunzip decompression parameters
05 Lua control structure
Nacos 详解,有点东西
Day (2) of picking up matlab
安装Redis并部署Redis高可用集群
捡起MATLAB的第(2)天
RAID磁盘阵列与RAID5的创建
Jour (9) de ramassage de MATLAB
Disk management and file system
Day (5) of picking up matlab
The most detailed Backpack issues!!!
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
About background image gradient()!