C Programming

Uses of the Fprint() and Fscanf Functions

In today’s topic, we will discuss the implementation and usage of the fprintf() function and fscanf() function. The file is an important topic in the C language. It has two standard predefined functions. They are fprintf() and fscanf() functions. We will discuss these functions in detail below.

Comparison Between printf() and fprintf() Functions

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

With the help of the printf() function, we get user-friendly output. Its output goes to the monitor.

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

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

If we want to get formatted or structured outcome, we can use fprintf() function. Its declaration prototype is:

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

Programming Example 1

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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 the fopen() function and access it through a pointer named *fp. This file is opened in “w” mode. We know that in a file 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 the fprintf() function. The fclose() function will systematically close the file.

Programming Example 2

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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 in “w” mode we can write on the file. Then, we take some inputs from the user of some names of dogs and print those names in the file with the help of fprintf() function.

Programming Example 3

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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 the fopen() function and access it through a pointer named *fp. This file is opened in “w” mode. We know that a file in “w” mode, we can write on the file. Then, we take some strings in the file and print them with the help of the fprintf() function. We have to end that type of program by calling the fclose() function.

Reading From a File Using fscanf() Function

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

It will pick the data systematically from the stream and put them in its specified memory location.

Here, we will see an example of the fscanf() function:

Programming Example 4

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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, 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 the fscanf() function.

Programming Example 5

We will see another example of the fscanf() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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, 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 the fscanf() function.

Programming Example 6

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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.

Conclusion

In this article, we discussed the different programming examples of the fprintf() and fscanf() functions. Watching these examples, we have come to the conclusion that the application of those functions is helpful in handling the different types of files in the C language. These functions are required in the various modes of 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