Fscanf Not Reading Anything From a File

fscanf() in C

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 tin say read a set of characters from the stream or a file. This role reads the stream in the form of byte after that interprets the input according to the format and for the output, they shop the format into their statement. It basically reads from a file besides contain a pointer i.e. file arrow and then it read a specific surface area or office 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.east. stream and format. Permit'due south talk over them each in detail;

  • Format: This format contains diverse placeholders that we used to read input. We tin talk over them each in detail in the coming department.
  • 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 role takes two parameter streams and formats. This stream is the arrow to the file and format incorporate a listing of placeholder which is used to read the specific type of data.

Let'due 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.g>> 2.04000e+01

2. %f: This placeholder in C language is used to read the floating numbers every bit well but this will exist in a fixed decimal format only.

for eastward.g. >> 13.0000006

3. %g: This placeholder in C language is used to read the floating numbers only. Simply 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 about 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 i digit.

half-dozen. %south: This placeholder in C language is used to read a cord of characters. This is going to read the stream until information technology finds any blank tan or newline. In other words, we 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.

viii.%10: 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 cardinal points while working with the fscanf role in C language:

1. We need to include the header while working with it. #include <stdio.h> This header should exist there otherwise error will be generated.

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

3. Nosotros have similar functions available in C like fscanffunction which is as follows:

  • sscanf()
  • scanf()

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

one. width: This statement specifies the width of the characters that needs to exist read in the current operation. This can exist the maximum number of inputs.

2. *: This argument is used to indicate that the data is to be read from the stream.

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

fscanf function return value: This part returns the graphic symbol that we stored and read from a file. If this role not able to read any detail from a file and end of file occurs or an mistake occurs and then this function volition return EOF. The main advantage is it does not read the whole file it but read according to our logic.

Examples to Implement fscanf() in C

Below are the examples of fscanf() in C:

Instance #1

In this example, we are trying to create one file and read the name of the bloom and color of the flower. We accept created on the file named demo.txt.

Lawmaking:

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

Output:

fscanf() in C Example 1

Example 1

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");
render;
}
printf("Name of the student \due north");
scanf("%southward", studentName);
fprintf(filePointer, "Proper noun= %s\north", studentName);
printf("Accost of the student \due north");
scanf("%southward", studentAddress);
fprintf(filePointer, "Address= %s\n", studentAddress);
fclose(filePointer);
}

Output:

fscanf() in C Example 2

Example 2

Example #3

Effort to read different parameters from the file.

Code:

#include <stdio.h>
void master()
{
FILE *filePointer;
char bankName[30];
char bankAddress[30];
char rate[thirty];
char amount[thirty];
filePointer = fopen("student.txt", "w+");
if (filePointer == Aught)
{
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("Accost of the bank \n");
scanf("%s", bankAddress);
fprintf(filePointer, "Accost= %south\n", bankAddress);
printf("rate of the banking concern \n");
scanf("%s", rate);
fprintf(filePointer, "Charge per unit= %s\n", rate);
printf("amount of the bank \n");
scanf("%s", corporeality);
fprintf(filePointer, "Corporeality= %due south\n", amount);
fclose(filePointer);
}

Output:

File Pointer Example 3

Example 3

Conclusion

Fscanf role is used to read data from the file.  Information technology takes 2 parameter streams and formats. We can format our data using various placeholders co-ordinate to the blazon of input nosotros want to read from the file. This office is helpful when we want to read specific data from the file and practice not need to read the whole stream.

Recommended Manufactures

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. Y'all can also go through our other suggested articles to acquire more –

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

randallgairciand.blogspot.com

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

0 Response to "Fscanf Not Reading Anything From a File"

Mag-post ng isang Komento

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel