Table of Contents
show
The fscanf() function is used to read set of characters from file. It reads a word from the file and returns EOF at the end of file.
Syntax
fscanf
int fscanf(FILE *stream, const char *format, …)
Parameter
stream − This is the pointer to a FILE object that identifies the stream.
format − This is the C string that contains one or more of the following items − Whitespace character, Non-whitespace character and Format specifiers.
Return value
This function returns the number of input items successfully matched , else 0
Program: reading data from file using fscanf
#include <stdio.h>
#include<stdlib.h>
int main()
{
int n;
FILE *fptr;
if ((fptr=fopen("sample.txt","r"))==NULL)
{
printf("Cannot Open File ");
exit(1);
}
fscanf(fptr,"%d",&n);
printf("Value in file is=%d",n);
fclose(fptr);
}
Input
Output
Views: 0