Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. File Processing
  5. File Processing Operation...
  6. Read set of chars from file

Read set of chars from file

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

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments