Previous Page
Next Page

vfscanf, vscanf, vsscanf

Reads formatted data using a variable argument list

#include <stdio.h>
#include <stdarg.h>
int vfscanf ( FILE * restrict fp , const char * restrict format ,
             va_list argptr  );
int vscanf ( const char * restrict format , va_list argptr  );
int vsscanf ( char * restrict buffer , const char * restrict format ,
             va_list argptr  );

The functions vfscanf( ), vscanf( ), and vsscanf( ) work in the same way as fscanf( ), scanf( ), and sscanf( ), respectively, except that their final argument, argptr, is a variable-argument list object with type va_list. The program must initialize this object by calling the va_start macro before calling the vfscanf( ), vscanf( ), or vsscanf( ) function, and must call the va_end( ) macro after the function returns. Because these functions use the va_arg( ) macro internally to advance the pointer through the argument list, its value is indeterminate after the vfscanf( ), vscanf( ), or vsscanf( ) function call has returned.

The va_start( ), va_arg( ), and va_end( ) macros and the type va_list are declared in the header file stdarg.h.


Like the fscanf( ), scanf( ), and sscanf( ) functions, vfscanf( ), vscanf( ), and vsscanf( ) return the number of input items that have been assigned to variables referenced by elements of the argument list.

Example

typedef struct {
  char lastname[20];
  char firstname[20];
  int dob_month;
  int dob_day;
  int dob_year;
} person;

person employee;

int read_person( char *lname, char *fname, ... )
// As variable arguments (...) use NULL
// or three int pointers (month, day, year).
{
  va_list args;
  int count;

  printf( "Enter the last name and first name (Example: Smith, Sally)\n" );
  count = scanf( "%[^,], %[^\n]", lname, fname );   // Read the name.

  va_start( args, fname);   // Initialize args to start with the argument
                            // that follows fname in the function call.
  if ( count == 2 && va_arg(args, int*) != NULL )
  {
    va_end( args);
    va_start( args, fname);  // Initialize args again.

    printf( "Enter the date of birth. (Example: 9/21/1962)\n");
    count += vscanf( "%d/%d/%d", args );        // Read date of birth.
  }
#ifdef DEBUG
  fprintf( stderr, "Read %d fields.\n", count);
#endif // def DEBUG

  va_end( args );
  return count;
}

int main( )
{
  person *pEmployee = &employee;
  int result;

  result = read_person( pEmployee->lastname,
                        pEmployee->firstname,
                        &pEmployee->dob_month,
                        &pEmployee->dob_day,
                        &pEmployee->dob_year );

#ifdef DEBUG
  fprintf( stderr, "Fields read: %s, %s; born %d-%d-%d\n",
           pEmployee->lastname,
           pEmployee->firstname,
           pEmployee->dob_month,
           pEmployee->dob_day,
           pEmployee->dob_year );
#endif // def DEBUG
}

See Also

va_start( ), va_arg( ), va_copy( ) and va_end( ); fscanf( ), scanf( ), and sscanf( ); vfwscanf( ), vwscanf( ), and vswscanf( )


Previous Page
Next Page