Files - 6 fprintf() & fscanf() functions

fprintf() function :

It is similar to printf(), printf()  prints the data on the standard  output but fprintf() prints the data  into specified file stream. putc()  function was to write only  characters into files. fprintf() can  be used to write different types of  values into a file. 

Syntax:

fprintf ( FILE * stream, const char *  format, ... );

Description :

FILE *stream argument indicates  that in which file the data has to be  written.

const char *format indicates a  string that contains a type of data  to be written into the file.  In the  printf statement we specify a  control string to indicate the type  of value to be printed on standard  output in the same way here also  we specify format specifiers which  is a series of character formed by  prefixing a percentage sign (%). It  is used to specify the type and  format of the data to be written  into the file stream.

fscanf() function :



It is similar to scanf(), the basic  difference is that scanf() reads  data always from standard input  where as fscanf() reads data using  a file stream. getc() function is used for reading only character data whereas fscanf() allows us to read different types of data from file.

Syntax :


fscanf ( FILE * stream, const char *  format, arguments );

Description:

FILE *stream argument indicates  that from which file the data has to  be read.


const char *format indicates a  string that contains a series of  characters that control how  characters extracted from the the  file are treated. Usually in the  scanf statement we specify a  control string to indicate the type  of value we are reading in the  same way here also we specify  format specifiers which is a series  of character formed by prefixing a  percentage sign (%). It is used to  specify the type and format of the  data to be retrieved from the  stream and stores into the  memory locations pointed by  arguments specified in the fscanf() function.

Comments