C language Format file input and output

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.