Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The fscanf() function

The function fscanf() is used to read data from the file instead of a console. File pointer fpt is used to point the file from which data will be read.

Syntax

fscanf(FILE *fpt, const char *format [arg1, arg2, …, argN])

Inside file6.txt

C Programming Language fscanf Function in File Handling Example

Example

#include<stdio.h>
int main()
{
    FILE *fp;
    int id_no;
    char name[50];
	
    fp=fopen("D:\\C Programming\\file6.txt","r");
	
    if(fp==NULL)
    {
        printf("\n Unable to open the file");
        return 1;
    }
	
    while(fscanf(fp,"%s %d",name,&id_no)!=EOF)
    {
        printf("\n\n Name of Employee = %s",name);
        printf("\n\n ID of Employee = %d",id_no);
    }
    fclose(fp);
	
    return 0;
}

Output

C Programming Language fscanf Function in File Handling Example Output
 

Leave a reply