C Programming language

adplus-dvertising
Formatted I/O : How to use fscanf( ) Function in C
Previous Home Next

fscanf Function Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.

fscanf (FILE *fptr , const char *format [,address,.....]);

The fscanf function works exactly like the scanf function except that it reads the information from the stream specified by stream instead of standard input device.

fscanf function Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.

/**program to understand the use of fscanff() */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main( )
{
	clrscr();
	FILE *fp ;
	struct emp
	{[an error occurred while processing this directive]
	char name[50] ;
	int age ;
	float sal ;
	} ;
	struct emp e ;
	fp = fopen ( "employee.txt", "r" ) ;
	if ( fp == NULL )
	{
	puts ( "Cannot open file" ) ;
	exit( ) ;
	}
	while ( fscanf ( fp, "%s %d %f", e.name, &e.age, &e.sal ) != EOF )
	printf ( "\n%s %d %f", e.name, e.age, e.sal ) ;
	fclose ( fp ) ;
};

Output

Ashish 34 1250.500000

Anurag 21 1300.500000

Rajesh 27 1400.500000

Previous Home Next