C-language file pointer (similar to Java in document file = new file ();)
In the C language, a pointer variable is used to point to a file, which is called the file pointer format: The pointer variable identifier;
Opening of the file (fopen function)
The fopen function is used to open a file whose general form of invocation is: The file pointer variable name = fopen (file name, using file mode); Where: "file pointer name" must be a pointer variable that is described as a file type; The file name "is the file name" used by file " refers to the type of files and operation requires " file name "is a string constant or an array of strings
Close function of file (fclose function)
Once the file has been used, the closed file function is applied to close the file to avoid errors such as data loss of the file fclose the general form of the function call is: fclose (file pointer); Note: When you close the file operation normally, the Fclose function returns a value of 0. If a non-0 value is returned, it indicates that an error occurred if the file write operation did not close, it may cause file write failure
Cases:
Steps to manipulate the file: 1) Introduce the header file Stdio.h 2) to establish the file pointer *fp = NULL; 3) Open File fopen (file name, operation mode); Fp=fopen ("A.txt", "R");//r is the mode of operation//if open successfully, will return the first address of the file failed, will return null; 4) operation file 5) Close file fclose (file pointer);
How files are used:
R: Open an existing file and prepare to read the data from the file. Cannot write data to file W: Creates a new file and prepares to write data to the file. Cannot read data from a file, if the file already exists, this file will be overwritten A: Open an existing file, Prepare to append data at the end of the file. You cannot read data from a file, and if the file does not exist, the file is created to write to the data r+: Open an existing file, ready to read and write. That is, you can read data or write Data w+: Create a new file, prepare to read and write, Overwrite file if file already exists A +: equivalent to a, but can read data from a file T: open a text file B: open a binary file
Documentation Considerations:
1) File usage by R, W, A, T, B and six characters, the meaning of each character is: R (Read): Read W (write): Write A (append): Append T (text): text file, can omit do not write B (banary): binary file +: Read and write 2) where a file is opened with "R", the file must already exist and can only be read from the file 3) files opened with "W" can only be written to the file. If an open file does not exist, the file is created with the specified file name, and if the open file already exists, the file is deleted and a new file is rebuilt 4) to append new information to an existing file, you can only open the file in "a" mode. If the text Does not exist, it will automatically help us create 5) When you open a file, fopen returns a null pointer value NULL if an error occurs. This information can be used in the program to determine whether to complete the work of the open file, and to do the appropriate processing. Therefore, the following sections are used to open files: For example://define file pointer *f p = fopen ("A.txt", "R"); if (fp!=null) {//File open successfully}else{//File open failed printf ("file read Fail! Press any key to exit ~ "); Getch ();//wait for the user to enter a character, and then exit exit (1);//Program Exit} The meaning of this program is that if the returned pointer is empty, indicating that the A.txt file cannot be opened, the Given a hint, the next line of Getch () is to enter a character from the keyboard, but it is not displayed on the screen. Here, the function of the line is to wait, the program will continue to execute only when the user knocks from the keyboard, so the user can use this waiting time to read the error prompt. Execute exit after knocking (1) Exit program 6) Read the memory of a text file, when the ASCII code is converted to binary code, and the file is written to the disk in text, the binary code is converted to ASCII code, so the text file read and write takes more conversion time. There is no such conversion to the binary file 7) standard input file (keyboard), standard output file (display) , the standard error output (Error message) is open by the system and can be used directly
BYTE read and write functions: Fgetc and FPUTC
Reads one byte at a time read to EOF means the end of the file is reached and the read ends
string read and Write functions: Fgets and Fputs
Fputs has a return value that represents the number of words currently written fgets ("array name", "bytes read", "file pointer"); Note: 1) fgets read, when the contents of the file is read, the string will be added to the Terminator ( 2) fgets read, encountered \ n or EOF, read the end
Data block read-write function fread and fwrite
The C language also provides read and write functions for the entire block of data. Can be used to read and write a set of data, such as an array of elements, a structure variable value, such as the general form of a read block function call: fread (BUFFER,SIZE,COUNT,FP); The general form of a write block function call is: fwrite (BUFFER,SIZE,COUNT,FP); Where: buffer: Is a pointer, in the Fread function, which represents the first address of the input function. In the Fwrite function, it represents the first address that holds the output data size: The number of bytes representing the function block count: Represents the number of block blocks to read and write fp: represents a file pointer
Formatting writes to fprintf and FSCANF
Using the fprintf function, format writes data to the file fprintf (FP, "%d#%d", A, b); Write data in a specific format fscanf (FP, "%d#%d", b);
Random Read and write implementation of files
The key to implement random read and write is to move the position pointer as required, which becomes the position of the file to move the internal location of the pointer function mainly by two, namely rewind () and fseek () fseek function and rewind function Rewind (file pointer) re-set the file pointer to the first position of the file fseek function format: fseek (file pointer, displacement amount, starting point); File pointer: Pointer to the moved file Displacement: Indicates the number of bytes moved, the amount of displacement is a long data, so that the length of the file is greater than 64KB is not error, when the displacement with a constant, it is required to add the suffix "L" starting point: Where to begin to calculate the amount of displacement, There are three types of starting points: file first, current position and end file seek_set number 0 Current position seek_cur number 1 file end seek_end Number 2 example: fseek (fp,100l,0); The meaning is to move the position pointer to the first 100 bytes of the file Note: The fseek function is generally used for binary files, in the text file due to the conversion, it is often calculated position will appear error file end detection function feof function format: feof (file pointer); Function: Determine whether the file is at the end of the file, such as the end of the file, return to 1, otherwise 0 Note: This method will be performed more than once, the general preferred EOF detection error message: ferror function ferror ( file pointer); File error flag and file End flag 0 function clearerr function call format: clearerr (file pointer) function : This function is used to clear the error flag and the file end flag, so that they are 0 values
File-related actions in C language