Chapter Contents

Previous

Next
vsscanf

vsscanf



Read Formatted Input from a String

Portability: C99


SYNOPSIS
DESCRIPTION
RETURN VALUE
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

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

int vsscanf( const char *source, const char *format, va_list arg );


DESCRIPTION

vsscanf reads formatted input text from the string addressed by source. No file input is performed. 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

vsscanf is functionally equivalent to sscanf, 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(const char *s, 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",
   ""
};

int main()
{
   int i;
   float amount;  
   char unit[20];
   int count;


   for (i = 0 ;; i++)
   {
      count = DoScan(lines[i], "%f %s", &amount, unit);

      if (count == EOF)
         break;

      if (count < 2)
      {
         puts("Unexpected error in input data.");
         exit(EXIT_FAILURE);
      }

      printf("amount = %.1f, units = \"%s\"\n", amount, unit);
   }
   exit(EXIT_SUCCESS);
}


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

   va_start(args, fmt);
   n = vsscanf(s, fmt, args);
   va_end(args);

   return n;
}


RELATED FUNCTIONS

vsprintf, sscanf, 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.