Last Updated: Feb 3, 2025
Easy

fprintf() and fscanf() in C

Table of contents
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the C Programming Language, many pre-defined functions exist for different purposes. This article will discuss two inbuilt functions named the fprintf() function and the fscanf() function.

The purpose of the fprintf() function is to write formatted data to a file. The fprintf() function is almost identical to the printf() function. The only difference is that the fprintf() function writes data into the given file.

Moreover, an extra argument is also present in the fprintf() function. In the fprintf() function, the file pointer points to the file where the formatted output will be written, and if the write is successful, it will print the total count of characters. In comparison, the purpose of the fscanf() function is to read formatted data from a file. The fscanf() function reads the stream in the form of byte, interprets the input according to the format, and stores the format into their argument for the output. It reads from a file that also contains a pointer, i.e., file pointer, so it reads a specific area or part of the file instead of the whole stream.

Recommended Topics, Sum of Digits in C and C Static Function.

Syntax of fprintf() function

Syntax of fprintf() function is:

int fprintf(FILE *stream, const char *format, …)

 

Syntax of  fscanf() function

Syntax of fscanf() function is:

int fscanf(FILE *stream, const char *format, …)

 

In both functions' above-given syntax, input passes by two parameters. Those two parameters are as follows:

  • Stream:  A stream is a pointer to the file that indicates the file object which recognizes the file stream.

 

  • Format: It is the parameter that tells about how to format the data according to the user's requirements or to make the data easy to read. 
     

Also see, Short int in C Programming

Working of fprintf() function in C

There are some specific steps through which the fprintf() function works. Those steps are as follows:
 

  • First of all, we create a variable as per our requirement.

 

  • After that, we will open the text file in the defined location in write mode using a file pointer.

 

  • Suppose the file pointer is null. We made the system print an error message.

 

  • Suppose the file pointer is not null. We made the system execute the commands as per our requirements.

 

  • At last, we will open the file and check whether the command is executed successfully and whether the output present in the file is as expected or not.

Example of fprintf() function in C

Since the fprint() function writes the formatted data to a file, in this example we will see how the fprintf() function writes the formatted data in the file.

#include <stdio.h> 
main() 
{ 
   FILE *fptr; 
   fptr = fopen("mydoc.txt", "w");    //opening file
   fprintf(fptr, "Hello, I am Gaurav Bashisht.\n");    //writing data into file
   fclose(fptr);    //closing file  
}  
You can also try this code with Online C Compiler
Run Code

 

OUTPUT


Must Read what is storage class in c and Decision Making in C

Working of fscanf() function in C

fscanf() function of the C programming language reads a specific part of the file instead of reading the whole stream. For this process, the fscanf() function uses a file pointer. This function works on two parameters: streams and formats. This stream is the file's pointer, and the format contains a list of placeholders used to read the specific type of data. 

Example of fscanf() function in C

Since the fscanf() function reads the formatted data from a file, in this example we will see how the fscanf() function reads the formatted data from the file.

#include <stdio.h>

main()
{  
   FILE *fptr;
   char bgl[255];  //creating char array to store data of file
   
   fptr = fopen("mydoc.txt", "r");
   
   while(fscanf(fptr, "%s", bgl)!=EOF)
   {
   		printf("%s ", bgl );
   }
  
   fclose(fptr);
}
You can also try this code with Online C Compiler
Run Code

 

OUTPUT


You can also read about the dynamic arrays in c, and  Tribonacci Series

Must Read Passing Arrays to Function in C

Frequently Asked Questions

What is the syntax of the fprintf() function in C?

Syntax of fprintf() function is: int fprintf(FILE *stream, const char *format, …)

What is the syntax of the fscanf() function in C?

Syntax of fscanf() function is: int fscanf(FILE *stream, const char *format, …)

Why is the fprintf() function used?

The fprintf() function writes the character set into a file.

Why is the fscanf() function used?

The fscanf() function reads the formatted string from a given stream in the C programming language.

How many arguments does the fprintf() function and the fscanf() function take?

Both the functions take two arguments to pass the input to the program of the function.

Conclusion

The fprintf() and the fscanf() functions are standard inbuilt functions of the C programming language used in file handling. The fprintf() function returns a numerical value, the number of printed strings, whereas the fscanf() function returns the number of fields converted successfully. File handling is an essential task of the C programming language, and it has so many inbuilt functions which are related to print() and fscanf() functions like fopen(), fclose(), getw(), and putw(). To use these functions, we must learn about them very well.

Also read,