fclose function
The general form of the call is: fclose (file pointer), for example:
Fclose (FP); The Fclose function returns a value of 0 when the file operation is closed normally. The return of a value other than 0 indicates an error occurred. Reading and writing files is the most common file operation.
A variety of file-reading functions are available in C language:
• character read and write functions: Fgetc and FPUTC
• String Read and write functions: Fgets and Fputs
• Data block Read and write functions: Freed and Fwrite
• format Read and write functions: fscanf and Fprinf
The following are described separately. Using the above function requires that you include the header file stdio.h. Character read-write functions fgetc and FPUTC character read-write functions are read-write functions in characters (bytes). Each time you can read from a file or write a character to a file.
Fgetc of reading character function
The function of the FGETC function is to read a character from the specified file, in the form of a character variable =fgetc (file pointer), for example: Ch=fgetc (FP), which reads a character from the open file FP and feeds it into ch.
There are several explanations for the use of the FGETC function:
1. In the FGETC function call, the read file must be opened in read or read-write mode.
2. The result of reading a character can also not assign a value to a character variable, for example: fgetc (FP), but a read character cannot be saved.
3. There is a position pointer inside the file. The current read-write byte used to point to the file. When a file is opened, the pointer always points to the first byte of the file. When you use the FGETC function, the position pointer moves backward by one byte. So you can use the FGETC function multiple times to read multiple characters. Note that the file pointer and the position pointer inside the file are not the same thing. The file pointer is pointing to the entire file and must be defined in the program so that the value of the file pointer is unchanged as long as the value is not assigned. The position pointer inside the file is used to indicate the current read/write position within the file, which is moved backwards each time it is read and written, and is automatically set by the system without defining the description in the program.
[Example 10.1] reads the file e10-1.c and outputs it on the screen.
#include <stdio.h>
Main ()
{
FILE *FP;
Char ch;
if ((Fp=fopen ("e10_1.c", "RT")) ==null)
{
printf ("Cannot open file strike any key exit!");
Getch ();
Exit (1);
}
CH=FGETC (FP);
while (ch!=eof)
{
Putchar (CH);
CH=FGETC (FP);
}
Fclose (FP);
}
The function of this example program is to read characters from a file and display them on the screen. The program defines the file pointer fp, opens the file "e10_1.c" as a read text file, and causes the FP to point to the file. If there is an error opening the file, give the prompt and exit the program. The 12th line of the program reads a character first, and then goes into the loop, as long as the read character is not the end of the file (with an end mark EOF at the end of each file), the character is displayed on the screen, and then the next character is read. Each time you read it, the position pointer in the file moves back one character, and the pointer points to EOF when the file ends. Executing this program will display the entire file.