C Programming

Uses of fprintf() And fscanf() functions in C

This article will discuss the implementation and usage of fprintf() function and fscanf() function. File is an important topic in C language. It has two standard predefined functions: fprintf() and fscanf() functions.

Comparison between printf() and fprintf() function:

printf (“Sum is %d”, s);

With the help of printf() function we get formatted output which goes to the Monitor.

fprintf (fp, “Sum of %d and %d is %d”, a, b, c);

With the help of fprintf() function we get formatted output also which goes to the File.

fprintf () function is used to write formatted output to the specified stream. Its declaration protype is:

int fprintf (FILE *stream, const char *format [argument, …]);

Programming Example 1:

Here, we want to show how fprintf() function works in a file.

#include <stdio.h>
int main ()
{
    int a, b ;
    FILE *fp ;
    fp = fopen ( "f1.txt "," w " ) ;        // file opening mode.
    printf  (" Enter two numbers: \n " ) ;
    scanf ("%d %d", &a, &b ) ;
    printf (" \n ") ;
    fprintf (fp, "Sum of %d and %d is %d “, a, b, a + b ) ;   // uses of fprintf() function.
    fclose (fp) ;       // close the file.
return 0 ;
}

Output:

Explanation:

Here, we open a file named f1.txt with the help of fopen() function and access it through a pointer named *fp. This file is opened in “w” mode. We know that in a file with in “w” mode, we can write on the file. Then, we take some inputs from the user and print those variables a, b, and their sum (a+b) with the help of fprintf() function. After completing these operations, we close the file with the help of fclose() function.

Programming Example 2:

We will see another example of fprintf() function in the next programming example.

#include <stdio.h>

int main()
{
    FILE *fp ;
    fp = fopen(" example.txt "," w " ) ;        // file opening mode.
    char dog[ 5 ][ 20 ] = { "Bulldog", "Poodle", "German Shepherd", "Rottweiler", "Labrador Retriever" } ;

    fprintf (fp," Top 5 dog breeds are:\n ") ;      // uses of fprintf() function

    for (int i = 0 ; i<5 ; i++ )
        fprintf ( fp, " (%d) %s\n ", i+1, dog[ i ] ) ;
    fclose( fp ) ;          // close the file.
    return 0 ;
}

Output:

Explanation:

Here, we open a file named example .txt with the help of fopen() function and access it through a pointer named *fp. This file is opened in “w” mode. We know that in a file with in “w” mode, we can write on the file. Then we take some inputs from the user some names of dogs and print those names in the file with the help of fprintf() function. After completing these operations, we close the file with the help of fclose() function.

Programming Example 3:

In this programming example, we will see the last and final example of fprintf() function.

#include <stdio.h>
#include <stdlib.h>

int main ()
 {
   FILE * fp ;

   fp = fopen ( " myfile.txt ", " w " ) ;       // file opening mode.
   fprintf (fp, "%s %s %s %s", "Welcome", "to", "C", "Programming" ) ;  // uses of fprintf() function

   fclose ( fp ) ;          // close the file.

   return ( 0 ) ;
}

Output:

Explanation:

Here we open a file named myfile.txt with the help of fopen() function and access it through a pointer named *fp. This file is opened in “w” mode. We know that in a file with in “w” mode we can write on the file. Then, we take some strings in the file and print them with the help of fprintf() function. After completing these operations, we close the file with the help of fclose() function.

Reading from a file using fscanf ():

fscanf () is used to read formatted content from a File.

int fscanf (FILE *stream, const char *Format, …);

Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments.

Here we will see an example of fscanf() function.

Programming Example 4:

In this programming example, we will see an example of fscnf() function and its uses.

#include <stdio.h>
int main ()
{
    FILE *fp ;
    char b [ 100 ] ;
    fp = fopen ("f1.txt","r") ;         // file opening mode.
     while (fscanf (fp, "%s", b) != EOF)        // uses of fscanf()
{
    printf ( " %s ", b ) ;
     }
   fclose ( fp )
return 0// close the file.
}

Output:

Explanation:

Here, we will open a file named f1.txt in “r” mode. We know that if we open a file in read mode, that file must be present in the memory. Otherwise, it will not open. We open this particular file to read the content of the file. For this we use fscanf() function. After completing the operation, we close the file through fclose() function.

Programming Example 5:

We will see another example of fscanf() function.

#include <stdlib.h>
#include <stdio.h>

void allwords (FILE *) ;

int main ( void )
{
  FILE *fp ;

  if ( (fp = fopen("f1.txt", "r")) == NULL)         // file opening mode.
{
      perror ("Cannot open file!! ") ;
      exit( 1) ;
  }

  allwords( fp ) ;

  fclose( fp ) ;        // close the file.

  return 1 ;
}

void allwords (FILE * fp)
{
    char tmp [20] ;
    int i = 1 ;

    while (fscanf (fp, "%19s", tmp) != EOF){        // uses of fscanf()
        printf ( " Word %d: %s\n ", i, tmp ) ;
        i ++ ;
    }
return 0 ;
}

Output:

Explanation:

Here we will open a file named f1.txt in “r” mode. We know that if we open a file in read mode, that file must be present in the memory. Otherwise, it will not open. We open this particular file to read the content of the file. For this, we use fscanf() function. After completing the operation, we close the file through fclose() function.

Programming Example 6:

In this programming example, we will see the last and final example of fscanf() function.

#include <stdio.h>
#define  MAX_LEN  80
int main (void)
{
   FILE *fp ;
   long l ;
   float f ;
   char s [MAX_LEN + 1] ;
   char c ;
   fp = fopen ("count.txt", "r")// file opening mode.
   fscanf (fp, "%s", &s [ 0 ]) ;        // uses of fscanf().
   fscanf (fp, "%ld", &l) ;
   fscanf (fp, "%c", &c) ;
   fscanf (fp, "%f", &fp) ;
   printf ( " string = %s\n", s ) ;
   printf ( " long double = %ld\n ", l ) ;
   printf ( "char = %c\n ", c ) ;
   printf ( " float = %f\n ", f ) ;
return 0 ;
}

Output:

Explanation:

Here we will open a file named count.txt in “r” mode. We know that if we open a file in read mode, then that file must be present in the memory. Otherwise it will not open. We open this particular file to read the content of the file. For this we use fscanf() function. After completing the operation we will close the file through fclose() function.

Conclusion:

Here we will see different types of programming example of fprintf() and fscanf() function. Watching this example we have come to this conclusion that the application of those functions is really helpful to handle the different types of file in C language. These functions are required in the different modes in file handling.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com