Use the fgetc function to enter characters from the keyboard one by one, using the fputc function to write to the disk file.
Solution: Program:
#include <stdio.h>
#include <stdlib.h>//exit to terminate the program
int main ()
{
file* FP;
Char ch, filename[10];
printf ("Please enter the file name used:");
scanf ("%s", &filename);
if (fp = fopen (filename, "w")) = = = NULL)
{
printf ("Cannot open this file \ n");
Exit (0);
}
ch = getchar ();
printf ("Please enter a string ready to be stored to disk (end with #):");
ch = getchar ();
while (ch!= ' # ')
{
FPUTC (CH, FP);
Putchar (CH);
ch = getchar ();
}
Fclose (FP);
Putchar (10);//output a line break to the screen
return 0;
}
Results:
Please enter the name of the file used : File1.txt
Please enter a string to be stored to disk (end of # ): Yaoyao and xiaoxiao#
Yaoyao and Xiaoxiao
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1748102
C: Use the FGETC function to enter characters from the keyboard one by one, write to the disk file with the FPUTC function