Monday, October 19, 2020

, , ,

Example program for fscanf function in c

 fscanf() function in c programming is used to read input from a file. 

Well this function works same as the scanf() function in c programming but instead of read the given input in c program fscanf() function read the data from file.

Most of the arguments of fscanf() function in c are same as scanf() function.

  •  Return Value For fscanf() function program in c:-

This function return the number of value and complete the read operation or return the end of file EOF and -1 on error occurred.

 

Example program for fscanf() function in c


#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *f;
    char Name[50];
    int roll_num, chars;
    float mark;

    fp = fopen("records.txt", "r");

    if(f == NULL)
    {
        printf("Error opening file\n");
        exit(1);
    }

    printf("Testing fscanf() function: \n\n");
    printf("Name:\t\tRoll\t\tMarks\n");

    while( fscanf(f, "Name: %s\t\tRoll num: %d\t\tMarks: %f\n"
                    , Name, &roll_num, &mark) != EOF )
    {
        printf("%s\t\t%d\t\t%.2f\n", name, roll_no ,marks);
    }

    fclose(f);
    return 0;
}


Output of c program
Output of c program

0 comments:

Post a Comment