C programming vfscanf stdio.h


int vfscanf(FILE * restrict stream,
 const char * restrict format,
 va_list arg);

Parameters :
stream -The stream to which the data is to be outputted

format -This parameter will represent the entry point of the third argument.

arg -Stores the variable to which the data is to be written.

Return type
int-Returns the value of the macro EOF if an input failure occurs before any conversion.Otherwise, the vfscanf function returns the number of input items assigned,which can be fewer than provided for,or even zero, in the event of an early matching failure.

In C programming the <stdio.h> vfscanf function is equivalent to fscanf function but instead of using ellipsis as the third argument it is replaced by va_list macro (found in <stdarg.h> header ).A code example is given below.

Link : C fscanf stdio.h

Code example

void func(char* format , …)
{
int ret;

va_list arg ;

va_start(arg , format);

ret=vfscanf(stdin , format , arg);

printf(“ret=%d” , ret);

va_end( arg );
}

int main( )
{
int val=0;

func( “%d” , &val );

printf(“\nval=%d” , val);

getchar( );
return 0 ;
}

Output,

12
ret=1
val=12

The initial value of ‘val’ is 0 but calling ‘vfscanf’ and inputting 12 assign the value 12 to ‘val’,also note the second argument which is passed to the ‘func’ function must be a reference/address else the program will not work.





***Note all the conversion specifier rules still applies while using vfscanf,visit C programming scanf stdio.h for more information on all the conversion specifier rules.

A more exhaustive code example is given below.

Code example

void func(char* format , …)
{
int ret;

va_list arg ;

va_start(arg , format);

printf(“Enter day and date:”)

ret=vfscanf(stdin , format , arg);

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

va_end( arg );
}

int main( )
{
int date=0;

char day[20];

func( “%d” , day, &date );

printf(“\nDay=%s Date=%d” ,day , date);

getchar( );
return 0 ;
}

Output,

Enter day and date:Saturday 12
ret=2
Day=Saturday Date=12


Related links

->C vscanf stdio.h

->C vsscanf

->C sscanf

->C vfprintf stdio.h