[C language] Detailed explanation of file operation

1. Why use files

When we used the address book to record contacts, these contacts will be destroyed after the program ends, and these contacts need to be re-entered when the program is started next time. In order to prevent this situation, we introduce file operations. After the program ends, the content in the address book is saved in the file, and the content is read in the file when the program is started next time, so that data persistence can be realized.


2. What is a file

In programming, it is generally divided into: program files and data files .

1. Program files

It roughly includes source program files (.c), object files (.obj), executable programs (.exe), etc.

2. Data files

The data read and written when the program is running, such as the file from which the program needs to read data, or the file that outputs the content.

3. File opening and closing

1. File pointer

FILE* pf;

The definition pfis a FILEpointer variable pointing to data of type. It can point pfto the file information area of ​​a certain file (it is a structure variable). The file can be accessed through the information in the file information area. That is, through the file pointer variable can be found associated with it.

2. Opening and closing of files

文件的打开
FILE * fopen ( const char * filename, const char * mode );

文件的关闭
int fclose ( FILE * stream );

3. How to open the file

file usage ------------------------------meaning------------------- ----------- If the specified file does not exist
"r" (read-only) open a file read-only mistake
"w" (write only) open a file for writing only create a new document
“a” (append) open a file for appending create a new document
"rb" (read-only) open a binary file as read-only mistake
"rw" (write only) Open a binary file for writing only create a new document
“ra” (added) Open a binary file for appending create a new document
"r+" (read only) Open a file for reading and writing mistake
"w+" (write only) Create a file read-write create a new document
“a+” (append) Open a file for reading and writing for appending create a new document
"rb" (read-only) Open a binary file for reading and writing mistake
"wb" (write only) Create a binary file read-write create a new document
“ab” (added) Open a binary file for reading and writing for appending mistake

Fourth, the sequential reading and writing of files

1. scanf function

int scanf ( const char * format, ... );

scanf()函数Formatted input functions for the standard input stream (stdin)

2. printf function

int printf ( const char * format, ... );

printf()函数Formatted output functions for the standard output stream (stdout)

3. fscanf function

int fscanf ( FILE * stream, const char * format, ... );

fscanf()function formatted input function for all input streams (filestream/stdin)

4. fprintf function

int fprintf ( FILE * stream, const char * format, ... );

fprintf()函数Formatted output functions for all output streams (filestream/stdout)

5. sscanf function

int sscanf ( const char * s, const char * format, ...);

sscanf()Function to convert a string to formatted data

6. sprintf function

int sprintf ( char * str, const char * format, ... );

sprintf()Function to convert formatted data into a string

5. Random reading and writing of files

1. fseek function

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

fseek()函数for moving within an open fileread/writeThe position of the position pointer. It can set pointers anywhere in the file to read or write to different parts of the file. Specifically, fseek()函数three parameters are accepted: file pointer, offset, and starting point . By specifying these parameters, it is possible to locate anywhere in the file.

constant Location
SEEK_SET beginning of file
SEEK_CUR current position of the file pointer
SEEK_END end of file

2. ftell function

long int ftell ( FILE * stream );

ftell()函数It is used to return the offset of the current file pointer relative to the beginning of the file , that is, the number of bytes from the current position to the beginning of the file.

3. rewind function

void rewind ( FILE * stream );

rewind()函数Used to reposition the file pointer to the beginning of the file. This means that any subsequent read or write operations will start from the beginning of the file.

6. Judgment of the end of file reading

feof function

Notice:
feof()函数It is used to judge whether the read file ends normally, not whether it ends.
So feof()函数it should be used after the end of the file, not during reading.

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

end

If you have any suggestions and questions, or if there are any mistakes, I hope everyone can mention them.
I hope everyone can make progress with me in the future! !
If this article is useful to you, I hope you can give me a little like!

Guess you like

Origin blog.csdn.net/qq_55401402/article/details/130034580