File: the data set stored on the disk according to certain rules.
File Name: a string that uniquely identifies a disk file. Format: drive letter:/path/file name. Extension
Text File: data is stored on disk in one byte in the form of ASCII code of its numeric characters.
Binary File: the data is stored in binary format on the disk.
Device File: input/output device
Standard input file: keyboard
Standard output file/standard error output file: Display
File pointer: the C language uses the structure pointer named file to manage file read/write. File * <variable name>
File opening and closing: File Operations first establish the relationship between the file and the file pointer, and then read and write the file. The process of establishing a connection between a file and a file pointer is to open a file. To terminate this contact is to close the file.
File structure: defined in stdio. h. The format is as follows:
Typedef struct
{
Int _ FD;/* File Code */
Int _ cleft;/* number of bytes remaining in the File Buffer */
Int _ mode;/* file usage mode */
Char * nextc;/* The next byte address to be processed, that is, the internal pointer of the file */
Char * Buff;/* first address of the File Buffer */
} File;
2. The main operation functions are used accordingly.
1) open and close the file:
# Include <stdio. h>
File * FP;
Fp = fopen (char * filename, char * made );
If (FP = NULL)
{
Exit (0 );
}
Fclose (FP)
Modes and types include:
R: open a text file for reading data. If the file does not exist, null is returned.
W: Create a text file for writing. Discard the original content.
A: open or create a text file that appends data to the end of the file.
R +: update data
W +: update data and discard original content
A +: update the data and append the written data to the end of the file.
Add "B" to the binary"
File pointer of standard input/output files: stdin, stdout, stderr
2) File Reading and Writing:
Write a character into a file or read a character from the file: the internal pointer of the file automatically moves to the next readable position.
Int fputc (char CH, file * FP); // The written characters are returned successfully, and EOF is returned if a failure occurs.
Int fgetc (File * FP) // The written character is returned if the operation succeeds.
Write a row of data into a file or read a row of data:
Int fputs (char * STR, file * FP) // success returns 0
Char * fgets (char * STR, int N, file * FP); // read the N-1 characters, fill in '/0' for the n characters, and return the read string.
Write data of a specified number of bytes to a file or read the file.
Int fwrite (char * Buf, unsigned size, unsigned N, file * FP); size: the number of bytes of each data, n data. The number of written characters is returned successfully.
Int fread (char * Buf, unsigned size, unsigned N, file * FP); returns the number of characters read successfully.
Write Data in the specified format to a file or read data in the specified format.
Int fprintf (File * FP, char * format, E1, E2,... en); returns the number of written characters.
Int fscanf (File * FP, char * format, E1, E2,... en); returns the number of characters read successfully.
3) file location determination and pointer management.
Test at the end of the file: int feof (File * FP); // if the current file has reached the end of the file, non-0 is returned; otherwise, 0 is returned.
Different computer systems use different keyboard keys to form the file Terminator. For ibm pc and its compatible hosts, <Ctrl> + z
Relocates the internal pointer of the file to the starting position of the file, int rewind (File * FP );
Position the internal pointer of the file to the specified position: int fseek (File * FP, long offset, int from); // success returns 0
From is the starting point of positioning.
Seek_set 0 starts from the file header
Seek_cur 1 starts from the current position of the internal pointer of the file
Seek_end 2 starts from the end of the file
Offset is the number of bytes starting from. If it is greater than 0, it indicates that it is at the end of the file and 0 does not move,
File Operation Error Test int ferror (File * FP );
Used to test the correctness of the last operation on the file pointed to by FP. If an error is returned, a non-0 error is returned, and no 0 is returned.
4) delete an object
Int remove (char * filename );
3. Some knowledge points I did not expect before.
Char ch;
Ch = getchar () function is equivalent to CH = fgetc (stdin) function is equivalent to scanf ("% C", CH) function is equivalent to fscanf (stdin, "% C", CH)
Putchar (CH) is equivalent to printf ("% C", CH). It is equivalent to fputc (stdout) and fprintf (stdout, "% C", CH)
Note: There are some minor differences between these functions. Note the differences in their return values.