Skip to content Skip to sidebar Skip to footer

Read From a File in C Fscanf

fscanf() in C

Introduction to fscanf() in C

C fscanf function is used to read value from the file. This fscanf part used to read from the input stream or we can say read a prepare of characters from the stream or a file. This function reads the stream in the form of byte after that interprets the input co-ordinate to the format and for the output, they store the format into their argument. It basically reads from a file too incorporate a pointer i.due east. file pointer and 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, ...)

In a higher place is the syntax for declaring the fscanf function in C. It takes two parameters i.e. stream and format. Let'southward discuss them each in detail;

  • Format: This format contains various placeholders that we used to read input. Nosotros can discuss them each in item in the coming department.
  • Stream: This stream is the pointer i.due east. the file arrow where the output will end.

How fscanf() Function Works in C?

This function in C linguistic communication is used to read a specific part of the from the file instead of reading the whole stream. For this, information technology 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.

Allow'south hash out them in details:

1. %e: This placeholder in C is used to read the floating numbers. Merely in scientific notation.

for e.k>> ii.04000e+01

two. %f: This placeholder in C language is used to read the floating numbers too but this volition be in a fixed decimal format simply.

for e.g. >> xiii.0000006

3. %yard: This placeholder in C language is used to read the floating numbers simply. Just this floating number can be exponential or in decimal format depends upon the size of the input.

for eastward.g. >> xv.3

iv. %d: This placeholder is the near commonly used placeholder in C language. It is used to read the integer value.

for e.g. >> 5

v. %.1f: This placeholder in C linguistic communication is used to read the floating number only simply specific to fixed digit after decimal i.eastward. only 1 digit.

half dozen. %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, nosotros can say that it volition 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.%10: This placeholder in C language is used to read the value of hexadecimal Integer.

Primal Points of fscanf() in C

Only we demand to retrieve some central points while working with the fscanf part in C language:

1. We need to include the header while working with information technology. #include <stdio.h> This header should be there otherwise fault will be generated.

two. This fscanf office can be used with the following version: ANSI/ISO 9899-1990

3. We have similar functions available in C similar fscanffunction which is every bit follows:

  • sscanf()
  • scanf()

This function also takes various arguments which we can discuss below in details with their clarification;

1. width: This argument specifies the width of the characters that needs to be read in the electric current operation. This can be the maximum number of inputs.

ii. *: This statement is used to signal that the data is to be read from the stream.

three. type: This specifies the type of the information and which placeholder we need to read from the stream. It depends upon the blazon of information nosotros have.

fscanf role render value: This function returns the character that we stored and read from a file. If this function not able to read whatsoever particular from a file and stop of file occurs or an fault occurs so this role will return EOF. The main reward is information technology does not read the whole file it just read according to our logic.

Examples to Implement fscanf() in C

Beneath are the examples of fscanf() in C:

Example #1

In this example, nosotros are trying to create 1 file and read the proper name of the flower and color of the bloom. We take created on the file named demo.txt.

Code:

#include <stdio.h>
void main()
{
FILE *filePointer;
char fName[xxx];
char color[xxx];
filePointer = fopen("demo.txt", "w+");
if (filePointer == NULL)
{
printf("Requested file does not exists in system or not institute. \n");
return;
}
printf("Name of the flower \n");
scanf("%due south", fName);
fprintf(filePointer, "Name of the flower= %south\north", fName);
printf("Color of the flower \n");
scanf("%s", color);
fprintf(filePointer, "Color of the flower= %s\n", colour);
fclose(filePointer);
}

Output:

fscanf() in C Example 1

Example 1

Case #two

In this example, we are reading the data of students from the file.

Code:

#include <stdio.h>
void primary()
{
FILE *filePointer;
char studentName[xxx];
char studentAddress[30];
filePointer = fopen("student.txt", "w+");
if (filePointer == NULL)
{
printf("Requested file does non exists in system or non found. \north");
return;
}
printf("Proper name of the student \northward");
scanf("%s", studentName);
fprintf(filePointer, "Name= %due south\northward", studentName);
printf("Accost of the pupil \n");
scanf("%s", studentAddress);
fprintf(filePointer, "Address= %s\n", studentAddress);
fclose(filePointer);
}

Output:

fscanf() in C Example 2

Example 2

Example #3

Try to read different parameters from the file.

Code:

#include <stdio.h>
void chief()
{
FILE *filePointer;
char bankName[30];
char bankAddress[30];
char rate[30];
char amount[xxx];
filePointer = fopen("pupil.txt", "w+");
if (filePointer == NULL)
{
printf("Requested file does not exists in organisation or not constitute. \north");
render;
}
printf("Proper name of the bank \n");
scanf("%s", bankName);
fprintf(filePointer, "Proper noun= %due south\north", bankName);
printf("Address of the bank \n");
scanf("%due south", bankAddress);
fprintf(filePointer, "Accost= %s\north", bankAddress);
printf("rate of the banking company \due north");
scanf("%s", rate);
fprintf(filePointer, "Rate= %s\north", rate);
printf("amount of the banking company \north");
scanf("%s", amount);
fprintf(filePointer, "Amount= %s\n", amount);
fclose(filePointer);
}

Output:

File Pointer Example 3

Example 3

Decision

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 desire to read from the file. This function is helpful when we want to read specific data from the file and do non demand to read the whole stream.

Recommended Articles

This is a guide to fscanf() in C. Here we hash out the Introduction of fscanf() in C and how it works forth with dissimilar Examples and its Code Implementation. You tin can likewise go through our other suggested articles to learn more than –

  1. Prime number Numbers in C (Examples)
  2. How to Reverse Number in C?
  3. Introduction to Reverse Cord in C
  4. Reverse String in PHP | Loops

peircebothe1988.blogspot.com

Source: https://www.educba.com/fscanf-in-c/

Post a Comment for "Read From a File in C Fscanf"