Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax of fprintf() function
3.
Syntax of  fscanf() function
4.
Working of fprintf() function in C
5.
Example of fprintf() function in C
6.
Working of fscanf() function in C
7.
Example of fscanf() function in C
8.
Frequently Asked Questions
8.1.
What is the syntax of the fprintf() function in C?
8.2.
What is the syntax of the fscanf() function in C?
8.3.
Why is the fprintf() function used?
8.4.
Why is the fscanf() function used?
8.5.
How many arguments does the fprintf() function and the fscanf() function take?
9.
Conclusion
Last Updated: May 30, 2024
Easy

fprintf() and fscanf() in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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, …)

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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  
}  

 

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);
}

 

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,

 

Refer to our guided paths on Code360 to learn more about C programming, DSA, JavaScript, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

 

Next article
C fputc() and fgetc()
Live masterclass