当前位置:网站首页>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

1. Random reading of files

1.1 fseek

1.2 ftell

1.3 rewind 

2. Text files and binaries  

3. Determination of the end of file reading

4. File buffer  


1. Random reading of files

1.1 fseek

Locate the file pointer according to its position and offset

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

Used to return the offset of the file pointer relative to the starting position

#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 

Used to return the position of the file pointer to the starting position of the file

#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