Example 16.1
Output the text entered from the keyboard as-is to a file named File_a.dat, with the character @ as the keyboard input end flag.
(1) Open the file.
(2) Enter a character from the keyboard.
(3) Determine if the input character is @. If so, end the loop and perform step (7).
(4) Output the characters you have just entered into the specified file.
(5) Enter a character from the keyboard.
(6) Repeat steps (3) to (5).
(7) Close the file.
(8) End of procedure.
1#include <stdio.h>2#include <stdlib.h>3 4 Main ()5 {6FILE *fpout;7 Charch;8 if(Fpout = fopen ("File_a.dat","W")) ==NULL)9 {Tenprintf"Can ' t open this file!\n"); OneExit0); A } -CH =GetChar (); - while(ch! ='@') the { - FPUTC (CH, fpout); -CH =GetChar (); - } + fclose (fpout); -}
Example 16.2
Outputs the contents of a File_a.dat text file on an existing disk to the terminal screen as-is.
(1) Open the file.
(2) reads a character from the specified file.
(3) Determine if the read-in is the end-of-file flag. If so, end the loop and perform step (7).
(4) Output the characters just entered to the terminal screen.
(5) read one more character from the file.
(6) Repeat steps (3) to (5).
(7) Close the file.
(8) End of procedure.
1#include <stdio.h>2#include <stdlib.h>3 4 Main ()5 {6FILE *Fpin;7 Charch;8 if(Fpin = fopen ("File_a.dat","R")) ==NULL)9 {Tenprintf"Can ' t open this file!\n"); OneExit0); A } -CH =fgetc (fpin); - while(ch! =EOF) the { - Putchar (CH); -CH =fgetc (fpin); - } + fclose (fpin); -}
123
The National Computer Grade examination two level course-C Language Programming _ 16th _ file