[Basic knowledge of C language] File operation

What is a file

A file refers to a collection of data stored on an external storage. To be more precise, a file is an ordered collection of a group of related elements or data, and each collection has a symbolic reference, which is called the file name.

Classification of documents

There are generally two types of files in program design: program files and data files.

program files:

• Including source file (suffix .c), object file (windows environment suffix is ​​.obj), executable program (windows environment suffix is ​​.exe).

data files:

• The content of the file is not necessarily the program, but the data read and written when the program is running, such as the file that needs to read data from the program or the file that outputs the content.

According to the organization of the data, the data file is called a text file or a binary file . The data is stored in the memory in binary form. If the output is output to the external memory without conversion, it is a binary file.

File buffer

The ANSIC standard uses the " buffer file system " to process data files. The so-called buffer file system means that the system automatically opens a " file buffer " in the memory for each file in use in the program . The data output from the memory to the disk will be sent to the buffer in the memory first, and then sent to the disk together after the buffer is full. If you read data from the disk to the 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. The buffer size is determined by the C compilation system.

Insert picture description here

File pointer

In the buffer file system, the key concept is "file type pointer", or "file pointer" for short.
Each used file has a corresponding file information area in the memory, which is used to store the related information of the file (such as the name of the file, the file status and the current location of the file, etc.). This information is stored in a structure variable. The structure type is declared by the system, named FILE.
For example, the stdio.h header file provided by the VS2008 compilation environment has the following file type declaration:

struct _iobuf {
    
    
        char *_ptr;
        int   _cnt;
        char *_base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char *_tmpfname;
       };
typedef struct _iobuf FILE;

The general form of declaring a file pointer variable is:

FILE* 文件型指针变量名;

File opening and closing

The file should be opened before reading and writing, and the file should be closed after the end of use.
When writing a program, when opening a file, it will return a FILE* pointer variable to point to the file, which is equivalent to establishing the relationship between the pointer and the file.
ANSIC stipulates that the fopen function is used to open the file, and fclose is used to close the file.

FILE * fopen ( const char * filename, const char * mode );
int fclose ( FILE * stream );

There are many ways to open files, as follows:
Insert picture description here

Sequential reading and writing of files

Features Function name Apply to
Character input function fgetc All input streams
Character output function fputc All input streams
Text input function fgets All input streams
Text output function fputs All input streams
Format input function fscanf All input streams
Formatted output function fprintf All input streams
Binary input fread file
Binary output fwrite file

Random read and write of files

fseek:

Position the file pointer according to the position and offset of the file pointer.

int fseek( FILE *stream, long offset, int origin );

ftell:

Returns the offset of the file pointer relative to the starting position.

long int ftell ( FILE * stream );

rewind:

Return the position of the file pointer to the starting position.

void rewind( FILE *stream );

Judgment of the end of the file

feof:

feof() is a function to detect the end of file on the stream. If the file ends, it returns a non-zero value, otherwise it returns 0.

int feof( FILE *stream );


Keep in mind that feof is used incorrectly : in the process of file reading, the return value of the feof function cannot be used directly to determine whether the file is over.
Rather, it is used when the file reading ends to determine whether the reading fails or the end of the file is encountered.

  1. Whether the text file is read is finished, judge whether the return value is EOF (fgetc), or NULL (fgets)
    For example:
    fgetc judges whether it is EOF.
    fgets judges whether the return value is NULL.
  2. Judgment of the end of reading the binary file, and judge whether the return value is less than the actual number to be read.
    For example:
    fread judges whether the return value is less than the actual number to be read.

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/109696584