vfscanf()

Converts formatted text from a stream.

  #include <stdarg.h>
  
  #include <stdio.h>
  
  int vfscanf(FILE *stream, const char *format, va_list arg);    
Parameter

stream

A pointer to a file stream.

format

A format string.

arg

An argument list.

Remarks

The vfscantf() function works identically to the fscanf() function. Instead of the variable list of arguments that can be passed to fscanf(), vfscanf() accepts its arguments in the array arg of type va_list, which must have been initialized by the va_start() macro from the stdarg.h header file. The vfscanf() does not invoke the va_end macro.

vfscanf() returns the number of items assigned, which can be fewer than provided for in the case of an early matching failure. If an input failure occurs before any conversion, vfscanf() returns EOF.

This facility may not be available on configurations of the EWL that run on platformswithout file systems.

Listing: Example of vfprintf() Usage

#include <stdio.h>

#include <stdlib.h>

#include <stdarg.h>

int fsc(FILE *, char *, ...);

int main(void)

{

FILE *f;

int i;

double x;

char c;

int numassigned;

// create a new file for output and input

if (( f = fopen("foobar", "w+")) == NULL) {

printf("Can't create new file.\n");

exit(1);

}

// output formatted text to the filef

printf(f, "%d\n%f\n%c\n", 45, 983.3923, 'M');

// go to the beginning of the file

rewind(f);

// read from the stream using fscanf()

numassigned = fsc(f, "%d %lf %c", &i, &x, &c);

// close the file

fclose(f);

printf("The number of asssignments is %d.\n", numassigned);

printf("The integer read is %d.\n", i);

printf("The floating point value is %f.\n", x);

printf("The character is %c.\n", c);

return 0;

}

// fsc() scans an input stream and inputs

// a variable number of arguments using

// the vfscanf() function

int fsc(FILE *stream, char *format, ...)

{

va_list args;

int retval;

va_start(args, format); // prepare the arguments

retval = vfscanf(stream, format, args);

va_end(args); // clean the stack

return retval;

}

Output:

The number of asssignments is 3.

The integer read is 45.

The floating point value is 983.392300.

The character is M.