File operations in C language (remainder)

The word count of the last blog has reached the limit, so I will add the content to this article. We still have no introduction to the determination of the end of file reading and the content of the file buffer. Let us start the next study!

Table of Contents

1. Determination of the end of file reading

1.1feof function

1.2ferror function

code example

2. File buffer

2.1fflush function

code example


1. Determination of the end of file reading

As for the end of file reading, we need to judge whether it ends when it encounters an error or encounters the end-of-file flag. At this time, we generally use two functions, feof function and ferror function. Let us do Get to know them

1.1feof function

1. Function: Test the end of file on the stream (file stream or standard output stream)

2.Header file: #include

3. Use the format: int feof( FILE *stream );

4.Return value:

Returns a non-zero value if an attempt is made to read beyond the end of the file after the first read operation

If the current position is not the end of the file, return 0

5. But when we use this function, students often use it incorrectly. In the final analysis, it is still caused by unfamiliarity with this function. This requires us to calm down and take a closer look at its specific usage.

During the file reading process, we cannot use the return value of the feof function to directly determine whether the file is ended

It should be used when the file reading ends to determine whether the file ended due to a reading failure or the end of the file.

This means that feof is here to tell you the reason why the file reading failed, rather than returning a value to you

For example:

Whether the text file reading (output) is completed. If you use the fgetc function, you must determine whether the return value is EOF.

Whether the reading (output) of the binary file is completed. If the fread function is used, it is necessary to determine whether its return value is less than the actual number to be read.

The specific code of the function is implemented together with the ferror function

1.2ferror function

1. Function: Test errors in the stream (file stream or standard output stream)

2.Header file: #include

3. Use the format: int ferror( FILE *stream );

4.Return value:

If no error occurs, ferror will return 0.

If an error occurs, it will return a non-zero value.

Let’s look at a piece of code to deepen our understanding.

code example

//Judge the end of text file reading
int main()
{
int c = 0;
//Open this text file in read-only mode
FILE* pf = fopen("test2.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//The fgetc function will return EOF when reading the file fails or encounters the end-of-file flag.
while ((c = fgetc(pf)) != EOF)
{
putchar(c);
}
printf("\\
");
if (ferror(pf))//File reading failed and ended when an error was encountered
printf("I/O error when reading");
else if (feof(pf))//The file ends when it encounters the end mark
printf("End of file reached successfully");
fclose(pf);
pf = NULL;

return 0;
}

operation result

This shows that the file did end because it encountered the end-of-file mark, rather than encountering an error.

Let’s summarize:

feof: Returns true, indicating that the file ended normally after encountering the end-of-file mark

ferror: Returns true, indicating that the file ended due to an error during reading

2.File buffer

The ANSIC standard uses a “buffered file system” to handle data files.

The so-called buffered file system means that the system automatically opens up a “file buffer” in memory for each file being used in the program.

To output (write) data from memory to disk, the data will first be sent to the buffer in memory. When the buffer is full, the data will be sent from the buffer to disk

Inputting (reading) data from the disk to the computer will first read the data from the disk file and input it into the memory buffer. After the buffer is filled, the data will be sent from the buffer to the program buffer one by one

In addition, the size of the buffer is determined according to the C language compilation system

At this point, we know

Data cannot be placed directly into disk files. Only when

1. The buffer is full

2. Actively refresh the buffer

Only one of these two will put the data into the disk file.

This leads to a new function fflush, which is used to refresh the buffer. At this time, you may have questions again. The fflush function does not appear in the code example we wrote earlier. Why? It is written to the file. This is because the fclose function also refreshes the buffer when closing the file

Because of the existence of the buffer, when operating a file in C language, you need to refresh the buffer area or close the file at the end of the file operation. If you do not do this, it may cause file reading and writing failure and data loss. In addition, the size of the buffer can be Modified, we won’t explain too much here. We will explain it later when we encounter it.

2.1fflush function

1. Function: refresh buffer

2.Header file: #include

3. Use the format: int fflush( FILE *stream );

4.Return value:

Refresh successful: The return value is 0. If the specified stream has no buffer or is only opened for reading, the value 0 will also be returned

Refresh failed: return value EOF

Let’s look at an example to deepen our understanding

Code example

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<errno.h>

//Test buffer refresh function

int main()
{
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputs("hello world\\
", pf);//No refresh is performed, and the code is placed in the buffer at this time
printf("Sleep for 10s, we have written data, open the corresponding test.txt file and find that there is no content\\
");
Sleep(10000);
printf("Start flushing buffer\\
");
fflush(pf);//At this time, the data written to the buffer is placed in the file (disk)
printf("Sleep for 10 seconds. At this time, open the test.txt file again, and the written content appears in the file\\
");
Sleep(10000);
fclose(pf);//While closing the file, the buffer will also be refreshed
pf = NULL;

return 0;
}

We run the code and it appears first

Then we opened the corresponding file and found that there was indeed no content in the file, which proved that there was indeed a buffer.

Then we return to the program code running window, and a prompt appears

We open the corresponding file again and find that the content has been written in the file

This is all about file operations in C language. This part contains a lot of content. If you are interested, you must guard against arrogance and impetuosity, calm down and study seriously. The most important thing is to type more code and consolidate your knowledge!

OK! See you all next time! ! !

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge C Skill treeFileBasic operations of files 194818 people are learning the system