Chapter Contents

Previous

Next
vfscanf

vfscanf



Read Formatted Input from file

Portability: C99


SYNOPSIS
DESCRIPTION
RETURN VALUE
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdarg.h>
#include <lcio.h>

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


DESCRIPTION

vfscanf reads formatted input from the FILE designated by f according to the format specified by the string format. Following the format in the argument list is a pointer to a list of arguments, designated by arg, of type va_list as defined in the header <stdarg.h>.

The string pointed to by format is in the same form as that used by fscanf. See the fscanf description for detailed information concerning the formatting conventions.


RETURN VALUE

vfscanf returns EOF if end of file (or an input error) occurs before any values are stored. If values are stored, it returns the number of items stored; that is, the number of times a value is assigned to a member of the variable argument list.


IMPLEMENTATION

vfscanf is functionally equivalent to fscanf, except that the argument list has been replaced by a variable argument list, va_list, as defined in the header <stdarg.h>.


EXAMPLE

#include <stdarg.h>
#include <lcio.h>
#include <stdlib.h>

int DoScan(FILE *f, const char *fmt, ...);

static char *lines[] = {
   "147.8 pounds\n"
   "51.7 miles\n",
   "4.3 light-years\n",
   "10000 volts\n",
   "19.5 gallons\n"
};


main()
{
   FILE *tmpf;
   int i;
   __binfmt float amount;  // Declare a binary floating point number
   char unit[20];
   int count;

   tmpf = tmpfile();
   if (!tmpf)
   {
         puts("Couldn't open temporary file.");
         exit(EXIT_FAILURE);
   }
   for (i = 0; i < sizeof(lines)/sizeof(char *); ++i)
   {
      fputs(lines[i], tmpf);
   }
   rewind(tmpf);

   for(;;)
   {
      count = DoScan(tmpf, "%~bf %s", &amount, unit);
      if (feof(tmpf)) break;
      if (count < 2)
      {
         puts("Unexpected error in input data.");
         exit(EXIT_FAILURE);
      }
         printf("amount = %.1~bf, units = \"%s\"\n", amount, unit);
   }
   fclose(tmpf);
}

int DoScan(FILE *f, const char *fmt, ...)
{
   int n = 0;
   va_list args;

   va_start(args, fmt);
   n = vfscanf(f, fmt, args);
   va_end(args);

   return n;
}


RELATED FUNCTIONS

vfprintf, fscanf, va_arg, va_start, va_end


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2004 by SAS Institute Inc., Cary, NC, USA. All rights reserved.