What is the purpose of fscanf() in C?

This question was previously asked in
RRB JE ECE 22 Apr 2025 Shift 1 CBT 2 Official Paper
View all RRB JE Papers >
  1. Writes formatted data to a file
  2. Reads formatted data from a file
  3. Closes a file
  4. Reads a single character from a file

Answer (Detailed Solution Below)

Option 2 : Reads formatted data from a file
Free
General Science for All Railway Exams Mock Test
2.1 Lakh Users
20 Questions 20 Marks 15 Mins

Detailed Solution

Download Solution PDF

Explanation:

fscanf() Function in C

Definition: The fscanf() function in C is used for reading formatted input from a file. It is part of the standard I/O library and allows for data to be read from a file in a manner similar to how the scanf() function reads input from the standard input (keyboard).

Syntax: The syntax of the fscanf() function is as follows:

int fscanf(FILE *stream, const char *format, ...);

Here, FILE *stream is a pointer to a FILE object that identifies the input stream, const char *format specifies the format of the input, and the ellipsis (...) represents the additional arguments where the data read from the file will be stored.

#include 

int main() {
    FILE *file;
    int num;
    float fnum;
    
    file = fopen("data.txt", "r");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }
    
    fscanf(file, "%d %f", &num, &fnum);
    printf("Integer: %d, Float: %f\n", num, fnum);
    
    fclose(file);
    return 0;
}

In this example, the fscanf() function reads an integer and a floating-point number from the file data.txt and stores them in the variables num and fnum, respectively.

Latest RRB JE Updates

Last updated on Jun 7, 2025

-> RRB JE CBT 2 answer key 2025 for June 4 exam has been released at the official website.

-> Check Your Marks via RRB JE CBT 2 Rank Calculator 2025

-> RRB JE CBT 2 admit card 2025 has been released. 

-> RRB JE CBT 2 city intimation slip 2025 for June 4 exam has been released at the official website.

-> RRB JE CBT 2 Cancelled Shift Exam 2025 will be conducted on June 4, 2025 in offline mode. 

-> RRB JE CBT 2 Exam Analysis 2025 is Out, Candidates analysis their exam according to Shift 1 and 2 Questions and Answers.

-> The RRB JE Notification 2024 was released for 7951 vacancies for various posts of Junior Engineer, Depot Material Superintendent, Chemical & Metallurgical Assistant, Chemical Supervisor (Research) and Metallurgical Supervisor (Research). 

-> The selection process includes CBT 1, CBT 2, and Document Verification & Medical Test.

-> The candidates who will be selected will get an approximate salary range between Rs. 13,500 to Rs. 38,425.

-> Attempt RRB JE Free Current Affairs Mock Test here

-> Enhance your preparation with the RRB JE Previous Year Papers

Get Free Access Now
Hot Links: teen patti master 2025 teen patti classic teen patti joy official