当前位置:网站首页>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
边栏推荐
- vim编辑器的实时操作
- Detailed explanation of gzip and gunzip decompression parameters
- Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
- TIA botu - basic operation
- The most detailed Backpack issues!!!
- 撿起MATLAB的第(9)天
- Using JSON server to create server requests locally
- Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
- Use if else to judge in sail software - use the title condition to judge
- 各大框架都在使用的Unsafe类,到底有多神奇?
猜你喜欢
G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
Interview question 17.10 Main elements
各大框架都在使用的Unsafe类,到底有多神奇?
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago
第九天 static 抽象类 接口
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
Day (9) of picking up matlab
Cartoon: what are IAAs, PAAS, SaaS?
Gartner announces emerging technology research: insight into the meta universe
随机推荐
The font of the soft cell changes color
Day (5) of picking up matlab
JSP learning 1
Jour (9) de ramassage de MATLAB
You need to know about cloud disaster recovery
Sort by character occurrence frequency 451
下载并安装MongoDB
The most detailed Backpack issues!!!
Loading order of logback configuration file
MySQL的btree索引和hash索引区别
磁盘管理与文件系统
Win11/10家庭版禁用Edge的inprivate浏览功能
How magical is the unsafe class used by all major frameworks?
How to quickly batch create text documents?
04 Lua operator
NVIDIA显卡驱动报错
Day (10) of picking up matlab
05 Lua 控制结构
Introduction notes to PHP zero Foundation (13): array related functions
Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed