C programming fscanf stdio.h


In C programming <stdio.h> fscanf function read input from the stream.The declaration of the function is given below.

int fscanf(FILE * restrict stream ,
const char * restrict format, …);

Parameters:
stream -The stream from which the data is to be read.

format -It specify the format of reading the data.This format is usually a conversion specifier.

.. -An ellipsis-it consist of variable lists that stores the data read from the stream.

Return type
int – Returns the value macro EOF if an input failure occur but if successful returns the number of input items assigned which can be fewer than provided for or even 0 in the event of an early matching failure.

An example to read data from the file stream is given below.Note the file “text.txt” must exit and say it contains the string “Programmer”

Code example

FILE *fp=fopen( “text.txt” , “r+”);

char str[20] ;

int ret=fscanf( fp , “%s” , str);

printf( “%s \n%i” , str , ret);

fclose( fp );

Output,

Programmer
1

Another code example is shown below that read data from the console input.

Code example

char day[20] ;

int ret=0 , date=0;
float hr=0 ;

printf(“\nEnter date , day , hour \n”);
ret=fscanf(stdin , “%i %s %f”, &date , c , &hr);

printf(“\nret=%i”, ret);

printf(“\nDay:%s \nDate:%i \nHour:%f”, c , date , hr);

printf(“\nret=%i\n”, ret);

Output,

Enter date , day , hour

2 Monday 3

ret=3
Day:Monday
Date:2
Hour:3.000000





Some points to note

i)All the conversion specifier rules apply

While reading input from file or any other stream all the conversion specifier rules still apply.A three examples is given below.

Code example 1

wchar_t test[30] ;
int ii ;
double year ;

fscanf(stdin , “%3lf%d%ls%” , &year , &ii , test);

printf(“\n%lf %d %ls”, year, ii , test);

Suppose if the input is
1245   67   new

Then the output is,

124.000000   5   67

Here you can see that only the first three values of 124 is assigned to year,the ii is assigned the value 4 and the test is assigned the value 67.The next input will start with the value “new”.

Another code example with different case is given below.

Code example 2

char str[10];

fscanf( stdin , “abc%s” , str );

printf( “\nstr=%s” , str);

If the input is,
abc23 456

the Output is,

str=23

Note here the first three characters “abc” is neglected.

If using the above code the the input does not match with any of the three characters provided in the second argument the output is undefined.Consider the code example.

Code example 2

char str[10];

fscanf( stdin , “abc%s” , str);

printf( “\nstr=%s” , str);

If the input is,
abd23 456

the Output in Visual Studio is,

str=����������������������������

In the input the third character ‘d’ does not match the third character ‘c’ so the output is undefined.


ii)Failing conversion specifier rule

Whatsoever the case if the conversion specification does not match the behavior is undefined.


iii)When white space or newline is present when reading character

While reading characters from file or from the console if there is a white-space or new-line character the following characters are not read unless matched by a directive.The only successful way to suppress or determine the literal matches is by using the “%n” directive.

Code example 2

char str[10];

fscanf( stdin , “%s” , str);

printf( “\nstr=%s” , str);

Suppose if the input is

New star

The output is

New

Since the string “star” follows the white pace character it is not assigned to str,to avid such silly mistakes you can provide the directive “%n”.Consider the code below

Code example 2

char str[10] , str1[10] ;
int i ;

fscanf( stdin , “%s%n%s” , str , &i , str1);

printf( “\nstr=%s %s” , str , str1);

For the same input as the above code example the output is,

New star

The first string “New” is assigned to ‘str’, ‘i’ takes the white space character and str1 stores the “star” string.


Related links

->C scanf stdio.h

->C sscanf stdio.h

->C printf stdio.h