1) Overview
The C language regards files as a character sequence, which can be divided into ASCII files (text files) and binary files. A c file is a byte stream or binary stream.
Each byte in an ASCII file contains an ASCII code, which represents a single character. The output corresponds to the characters one by one, which facilitates character processing one by one, but occupies a large amount of space. Binary files are output to the disk as they are stored in the memory, saving space. Because the output does not correspond to the character, the character format cannot be output directly, which is generally used to save intermediate results. Currently, c can only process files in a buffer file system, that is, data must first pass through the buffer zone from the program to the disk file or from the disk file to the program, after the buffer zone is full, it is sent in a centralized manner.
2) folder type pointer
In a buffer file system, the key concept is file pointer. Because each used file opens a buffer in the memory to store information about the file. This information is stored in a struct variable. The struct type is defined by the system, named FILE, and defined in stdio. h.
FILE * fp;
Defines a file pointer variable fp, and subsequent operations on the file are performed through fp.
3) Open and Close a file
Before reading and writing a file, open the file.
The function to open the file is fopen (). The call method is as follows:
FILE * fp;
Fp = fopen (filename, using the file method );
If fopen () fails, a NULL pointer is returned. If it succeeds, a file pointer pointing to "filename" is returned and assigned to fp. In this way, fp is associated with the opened file. Or, fp points to "filename ".
File Usage: r, w, a, rb, wb, AB, r +, w +, a +, rb +, wb +, AB +. Remember the specific meaning.
4) close the file
To prevent data loss, close the opened file before the program ends, decoupling the file pointer from the file. Use the fclose (File pointer) function to close the file. After the function is executed, the data in the buffer zone is first sent to the disk file, and then the file pointer is released. 0 is returned for success, and 0 is returned for failure.
5) file read/write
After opening a file, you can read and write it. Common File Read and Write functions include:
① Fputc and fgetc
Fputc writes a character to a file in the format of fputc (ch, fp). It writes the character ch to the file pointed to by fp. This character is returned successfully. If it fails, EOF is returned. EOF is defined as the symbolic constant-1 in stdio. h.
Fgetc reads a character from a specified file, which must be opened in read or read/write mode. The call form is ch = fgetc (fp). Read a character from the file pointed to by fp and assign it to ch. When the file ends, fgetc returns an EOF. We can use the feof (fp) function) to determine whether the end of the file is reached. If the return value is 1, the end of the file is reached. Otherwise, 0 is returned. This function is applicable to text files and binary files.
② Fread and fwrite Functions
You can read and write a group of data. The call method is as follows:
Fread (buffer, size, count, fp );
Fwrite (buffer, size, count, fp );
Buffer is a pointer. For fread, it refers to the address for reading data from a file, and for fwrite, it refers to the address for writing data to a file.
Size: the number of bytes to read/write.
The number of size-byte data items to be read and written by count is actually the number of reads and writes.
Fp file pointer
The return values of these two functions are 1, and the failure is not 1. They are generally used for reading and writing binary files.
Note: Some c compilation systems do not have these two functions.
③ Fprintf () and fscanf () Functions
The formatting output and input functions are similar to printf () and scanf (), but they have only one difference. The read and write objects of fprintf () and fscanf () are not terminals but disk files. Call method:
Fprintf (File pointer, Format String, output list );
Fscanf (File pointer, Format String, output list );
④ Fgets () and fputs () Functions
The function is to read and write a string, such:
Fgets (str, n, fp );
It means to read n-1 characters from the file pointed to by fp, store them in str, and return the first address of str.
Fputs ("China", fp );
Write the string China into the file pointed to by fp. 0 is returned for success, and 0 is not returned for failure.
6) File Location
The file has a location pointer pointing to the current read/write location. to force change the location of the location pointer, you can use the relevant functions:
① Rewind causes the position pointer to return the beginning of the file again
② Fseek ()
The fseek () function can randomly change the position of the position pointer to achieve random file read and write. Call form:
Fseek (File pointer type, displacement, starting point );
The start point has the following three values:
Start with SEEK_SET or 0 file
Current location of SEEK_CUR or 1 file
SEEK_END or the end of file 2
The displacement refers to the number of bytes that are moved Based on the starting point (moving a positive number to the end of the file, and moving a negative number to the file header). Generally, the displacement uses long data to avoid file errors larger than 64 KB. The Fseek () function is generally used in binary files because character conversion is required for text files, which can lead to confusion during computation.
Fseek (fp, 100L, 0); moves the position pointer from the file header to the end of the file by 100 bytes.
Fseek (fp, 50L, 1); move the pointer from the current position to the end of the file 50 bytes.
Fseek (fp,-10L, 2); move the pointer 10 bytes from the end of the file to the file header.
③ Ftell ()
Obtain the current position of the streaming file position pointer. The displacement relative to the file header is returned successfully, and-1L is returned if the file fails.