Already familiar with the normal format input and output functions scanf and printf, the format file input and output function is to add F to the two-function names, indicating that "read-write objects are disk files rather than keyboards and monitors."
The prototypes for the two functions are:
int fprintf (FILE , const char , ...);
int fscanf (FILE , const char , ...);
fprintf and fscanf are almost identical to common printf and scanf usages, except that the first parameter-the file pointer, which identifies the input source file or the output's destination file-is the same.
When the input and output is correct, two functions return the number of characters correctly processed and EOF (-1) When an error occurs or when the end of a file is encountered.
Formatting i/o:fprintf and fscanf
Function Prototypes:
int fprintf (FILE *fp,const char *format[,argument,...])
int fscanf (FILE *fp,const char *format[,address,...])
function: I/O to files by format
Return value: Success, number of returned I/O, error or end of file, return EOF
Example fprintf (FP, "%d,%6.2f", i,t); Output I and T to the FP file in%d,%6.2f format
FSCANF (FP, "%d,%f", &i,&t); If the file has 3, 4.5, then 3 is fed into I, 4.5 into t
Example to enter data into a disk file from the keyboard by format
#include <stdio.h>Main () {Chars[ the],c[ the];intb; FILE *FP;if((Fp=fopen ("Test","W")) ==null) {puts("can ' t open file");Exit() ; }fscanf(stdin,"%s%d", S,&a);/*read from keaboard*/ fprintf(FP,'%s%d ', s,a);/*write to file*/Fclose (FP);if((Fp=fopen ("Test","R")) ==null) {puts("can ' t open file");Exit(); }fscanf(FP,"%s%d", c,&b);/*read from file*/ fprintf(STDOUT,'%s%d ', c,b);/*print to screen*/Fclose (FP);}
C language Format file input and output