Function Prototypes:
Char *fgets (char *s,int n,file *FP)
int fputs (char *s,file *FP)
Function: Read/write a string from the FP pointing to the file
return value:
Fgets returns the first address of the read string when normal, error or end of file, return null
Fputs returns the last character written when normal; error is EOF
Fputs writes the string that s points to the file that the FP points to
Fgets reads n-1 characters from the FP file into the memory area pointed to by S.
And at the end add a ' plus '
(Ends when a newline character or end of file (EOF) is encountered before n-1 characters are read in.)
Example reads a string from the keyboard into a file, and then reads back from the file display
#include <stdio.h>Main () {FILE *fp;Char string[Bayi];if((Fp=fopen ("File.txt","W")) ==null) {printf("Cann ' t Open file");Exit(0); } while(strlen(Gets (string)) >0) {fputs(string, FP);fputs("\ n", FP); } fclose (FP);if((Fp=fopen ("File.txt","R")) ==null) {printf("Cann ' t Open file");Exit(0); } while(Fgets (string,Bayi, FP)!=null)fputs(string, stdout); Fclose (FP);}
String I/O: fgets and fputs