Reading Values From a File Using Fscanf in C
Introduction to fscanf() in C
C fscanf role 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 class 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 comprise a pointer i.e. file pointer so information technology read a specific surface area or part of the file instead of reading the whole stream.
Syntax:
int fscanf(FILE *stream, const char *format, ...)
To a higher place 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 diverse placeholders that we used to read input. We can discuss them each in particular in the coming section.
- Stream: This stream is the pointer i.e. the file arrow where the output will stop.
How fscanf() Function Works in C?
This part 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 arrow to the file and format contain a listing of placeholder which is used to read the specific type of data.
Allow'due south discuss them in details:
i. %e: This placeholder in C is used to read the floating numbers. But in scientific notation.
for eastward.chiliad>> 2.04000e+01
2. %f: This placeholder in C linguistic communication is used to read the floating numbers equally well but this will be in a stock-still decimal format only.
for e.m. >> thirteen.0000006
3. %1000: This placeholder in C language is used to read the floating numbers simply. Only this floating number can be exponential or in decimal format depends upon the size of the input.
for eastward.one thousand. >> 15.3
4. %d: This placeholder is the nearly commonly used placeholder in C language. It is used to read the integer value.
for east.g. >> 5
5. %.1f: This placeholder in C language is used to read the floating number only but specific to stock-still digit after decimal i.east. but 1 digit.
vi. %s: This placeholder in C linguistic communication is used to read a string of characters. This is going to read the stream until information technology finds any blank tan or newline. In other words, nosotros tin say that it will read the stream to the whitespace.
7. %u: This placeholder in C linguistic communication is used to read the values of an unsigned decimal integer.
8.%10: This placeholder in C language is used to read the value of hexadecimal Integer.
Primal Points of fscanf() in C
Just nosotros demand to remember some key points while working with the fscanf function in C language:
i. 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 role tin can exist used with the post-obit version: ANSI/ISO 9899-1990
three. We have like functions bachelor in C like fscanffunction which is as follows:
- sscanf()
- scanf()
This function also takes various arguments which nosotros 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.
two. *: This argument is used to signal that the information is to exist read from the stream.
3. type: This specifies the type of the data and which placeholder we need to read from the stream. Information technology depends upon the type of data we have.
fscanf function return value: This function returns the character that nosotros stored and read from a file. If this function not able to read whatsoever item from a file and end of file occurs or an error occurs and so this role will return EOF. The chief reward is information technology does non read the whole file information technology just read according to our logic.
Examples to Implement fscanf() in C
Below are the examples of fscanf() in C:
Case #1
In this example, nosotros are trying to create one file and read the name of the blossom 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 colour[30];
filePointer = fopen("demo.txt", "w+");
if (filePointer == NULL)
{
printf("Requested file does non exists in system or not institute. \due north");
return;
}
printf("Name of the flower \north");
scanf("%s", fName);
fprintf(filePointer, "Name of the flower= %south\north", fName);
printf("Colour of the flower \n");
scanf("%s", color);
fprintf(filePointer, "Color of the blossom= %s\n", color);
fclose(filePointer);
}
Output:
Example #two
In this example, we are reading the information of students from the file.
Code:
#include <stdio.h>
void main()
{
FILE *filePointer;
char studentName[thirty];
char studentAddress[30];
filePointer = fopen("pupil.txt", "w+");
if (filePointer == Zip)
{
printf("Requested file does not exists in organisation or non found. \n");
render;
}
printf("Name of the student \n");
scanf("%s", studentName);
fprintf(filePointer, "Name= %s\northward", studentName);
printf("Accost of the student \due north");
scanf("%s", studentAddress);
fprintf(filePointer, "Address= %s\n", studentAddress);
fclose(filePointer);
}
Output:
Instance #3
Effort to read different parameters from the file.
Code:
#include <stdio.h>
void principal()
{
FILE *filePointer;
char bankName[30];
char bankAddress[30];
char charge per unit[thirty];
char amount[30];
filePointer = fopen("student.txt", "westward+");
if (filePointer == Goose egg)
{
printf("Requested file does non exists in arrangement or not found. \n");
render;
}
printf("Name of the bank \due north");
scanf("%s", bankName);
fprintf(filePointer, "Proper name= %s\n", bankName);
printf("Address of the bank \n");
scanf("%s", bankAddress);
fprintf(filePointer, "Address= %southward\northward", bankAddress);
printf("rate of the bank \n");
scanf("%due south", 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 office is used to read information from the file. It takes two parameter streams and formats. Nosotros tin can format our information using diverse placeholders according to the type of input we want to read from the file. This function is helpful when we desire 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 talk over the Introduction of fscanf() in C and how information technology works along with different Examples and its Code Implementation. You tin can as well go through our other suggested manufactures to learn more –
- Prime Numbers in C (Examples)
- How to Contrary Number in C?
- Introduction to Contrary String in C
- Reverse String in PHP | Loops
seaveywhisingerrim1994.blogspot.com
Source: https://www.educba.com/fscanf-in-c/
Post a Comment for "Reading Values From a File Using Fscanf in C"