Updated April 15, 2023
Introduction to fscanf() in C
C fscanf function is used to read value from the file. This fscanf function used to read from the input stream or we can say read a set of characters from the stream or a file. This function reads the stream in the form of byte after that interprets the input according to the format and for the output, they store the format into their argument. It basically reads from a file also contain a pointer i.e. file pointer so it read a specific area or part of the file instead of reading the whole stream.
Syntax:
int fscanf(FILE *stream, const char *format, ...)
Above is the syntax for declaring the fscanf function in C. It takes two parameters i.e. stream and format. Let’s discuss them each in detail;
- Format: This format contains various placeholders that we used to read input. We can discuss them each in detail in the coming section.
- Stream: This stream is the pointer i.e. the file pointer where the output will end.
How fscanf() Function Works in C?
This function in C language is used to read a specific part of the from the file instead of reading the whole stream. For this, it uses a file pointer. This function takes two parameter streams and formats. This stream is the pointer to the file and format contain a list of placeholder which is used to read the specific type of data.
Let’s discuss them in details:
1. %e: This placeholder in C is used to read the floating numbers. But in scientific notation.
for e.g>> 2.04000e+01
2. %f: This placeholder in C language is used to read the floating numbers as well but this will be in a fixed decimal format only.
for e.g. >> 13.0000006
3. %g: This placeholder in C language is used to read the floating numbers only. But this floating number can be exponential or in decimal format depends upon the size of the input.
for e.g. >> 15.3
4. %d: This placeholder is the most commonly used placeholder in C language. It is used to read the integer value.
for e.g. >> 5
5. %.1f: This placeholder in C language is used to read the floating number only but specific to fixed digit after decimal i.e. only 1 digit.
6. %s: This placeholder in C language is used to read a string of characters. This is going to read the stream until it finds any blank tan or newline. In other words, we can say that it will read the stream to the whitespace.
7. %u: This placeholder in C language is used to read the values of an unsigned decimal integer.
8.%x: This placeholder in C language is used to read the value of hexadecimal Integer.
Key Points of fscanf() in C
But we need to remember some key points while working with the fscanf function in C language:
1. We need to include the header while working with it. #include <stdio.h> This header should be there otherwise error will be generated.
2. This fscanf function can be used with the following version: ANSI/ISO 9899-1990
3. We have similar functions available in C like fscanffunction which is as follows:
- sscanf()
- scanf()
This function also takes various arguments which we can discuss below in details with their description;
1. width: This argument specifies the width of the characters that needs to be read in the current operation. This can be the maximum number of inputs.
2. *: This argument is used to indicate that the data is to be read from the stream.
3. type: This specifies the type of the data and which placeholder we need to read from the stream. It depends upon the type of data we have.
fscanf function return value: This function returns the character that we stored and read from a file. If this function not able to read any item from a file and end of file occurs or an error occurs then this function will return EOF. The main advantage is it does not read the whole file it just read according to our logic.
Examples to Implement fscanf() in C
Below are the examples of fscanf() in C:
Example #1
In this example, we are trying to create one file and read the name of the flower and color of the flower. We have created on the file named demo.txt.
Code:
#include <stdio.h>
void main()
{
FILE *filePointer;
char fName[30];
char color[30];
filePointer = fopen("demo.txt", "w+");
if (filePointer == NULL)
{
printf("Requested file does not exists in system or not found. \n");
return;
}
printf("Name of the flower \n");
scanf("%s", fName);
fprintf(filePointer, "Name of the flower= %s\n", fName);
printf("Color of the flower \n");
scanf("%s", color);
fprintf(filePointer, "Color of the flower= %s\n", color);
fclose(filePointer);
}
Output:
Example #2
In this example, we are reading the information of students from the file.
Code:
#include <stdio.h>
void main()
{
FILE *filePointer;
char studentName[30];
char studentAddress[30];
filePointer = fopen("student.txt", "w+");
if (filePointer == NULL)
{
printf("Requested file does not exists in system or not found. \n");
return;
}
printf("Name of the student \n");
scanf("%s", studentName);
fprintf(filePointer, "Name= %s\n", studentName);
printf("Address of the student \n");
scanf("%s", studentAddress);
fprintf(filePointer, "Address= %s\n", studentAddress);
fclose(filePointer);
}
Output:
Example #3
Try to read different parameters from the file.
Code:
#include <stdio.h>
void main()
{
FILE *filePointer;
char bankName[30];
char bankAddress[30];
char rate[30];
char amount[30];
filePointer = fopen("student.txt", "w+");
if (filePointer == NULL)
{
printf("Requested file does not exists in system or not found. \n");
return;
}
printf("Name of the bank \n");
scanf("%s", bankName);
fprintf(filePointer, "Name= %s\n", bankName);
printf("Address of the bank \n");
scanf("%s", bankAddress);
fprintf(filePointer, "Address= %s\n", bankAddress);
printf("rate of the bank \n");
scanf("%s", rate);
fprintf(filePointer, "Rate= %s\n", rate);
printf("amount of the bank \n");
scanf("%s", amount);
fprintf(filePointer, "Amount= %s\n", amount);
fclose(filePointer);
}
Output:
Conclusion
Fscanf function is used to read data from the file. It takes two parameter streams and formats. We can format our data using various placeholders according to the type of input we want to read from the file. This function is helpful when we want to read specific data from the file and do not need to read the whole stream.
Recommended Articles
This is a guide to fscanf() in C. Here we discuss the Introduction of fscanf() in C and how it works along with different Examples and its Code Implementation. You can also go through our other suggested articles to learn more –