Call the get (fgetc) and putc (fputc) functions for input and output. putcfputc
After the file is opened successfully, the next thing is to input or output the file. The simplest thing is to call getc (or fgetc) and putc (or fputc) function for character input and output.
1. Call the putc (or fpuct) function to output a character
The call form of the putc function is as follows:
Putc (ch, fp );
Here, ch is a character to be output. It can be a character constant or a character variable, and fp is a file pointer. Putc (ch, fp) is used to write the character ch to the file indicated by the file pointer fp. If the output is successful, the putc function returns the output characters. If the output fails, an EOF value is returned. EOF is a symbolic constant defined in the stdio. h library function file. Its value is-1.
The call form and function of the fputc function are identical to that of the putc function.
Example 1 output the text entered from the keyboard to the file named file_a.dat as is, and use the character @ as the symbol for keyboard input to end.
(1) open a file
(2) input a character from the keyboard
(3) Determine whether the input character is @. If yes, end the loop. Perform step (7)
(4) output the entered characters to the specified file.
(5) input a character from the keyboard
(6) Repeat steps (3) to (5)
(7) close the file
(8) program ended
The procedure is as follows:
1 # include <stdio. h> 2 # include <stdlib. h> 3 4 int main () 5 {6 FILE * fpout; 7 char ch; 8 if (fpout = fopen ("F: \ WEB \ TEST \ file_a.dat "," w ") = NULL) 9 {10 printf (" Cant't open this file! \ N "); 11 exit (0); // use the exit function, which must contain the stdlib. h header file 12} 13 ch = getchar (); 14 while (ch! = '@') 15 {16 putc (ch, fpout); 17 ch = getchar (); 18} 19 fclose (fpout); 20 return 0; 21}View Code
Enter abcdefghijklmnopqrstuvwxyz @ on the screen and @ as the input end sign.
The output result of the file file_a.dat is as follows:
2. Call the getc (or fgetc) function to input a character.
The call method of the getc function is as follows:
Ch = getc (fp );
Fp is the file pointer. The function reads a character from the file specified by fp and returns it as a function value. In the above expression, the getc function assigns a character read from the file to the variable ch.
The call form and function of the fgetc function are identical to that of the getc function.
Example 2 output the content in the existing file_a.dat text file to the terminal screen as is.
(1) open a file
(2) read a character from a specified file
(3) Determine whether the read object is the end mark of the file. If so, execute the following steps: (7)
(4) output the entered characters to the terminal screen
(5) read another character from the file
(6) Repeat steps (3) to (5)
(7) close the file
(8) program ended
Note: No matter which function is called to read files, you must first perform a read operation before judging whether the file is complete.
The procedure is as follows:
1 # include <stdio. h> 2 # include <stdlib. h> 3 4 int main () 5 {6 FILE * fpin; 7 char ch; 8 9 if (fpin = fopen ("F: \ WEB \ TEST \ file_a.dat "," r ") = NULL) 10 {11 printf (" Can't open this file! \ N "); 12 exit (0); 13} 14 15 ch = getc (fpin); 16 // you can use the feof function to determine whether the file is over, 17 // This function applies to text files and binary files 18 // if the return value of the function is 1, the end of the file is finished; if the return value is 0, the end is not 19 while (ch! = EOF) // or while (feof (fpin) = 0) 20 {21 putchar (ch); 22 ch = getc (fpin); 23} 24 fclose (fpin ); 25 26 return 0; 27}View Code
Run the program. The result is as follows: