RAD Studio
ContentsIndex
PreviousUpNext
sscanf, swscanf

Header File 

stdio.h  

Category 

Memory and String Manipulation Routines 

Syntax 

int sscanf(const char *buffer, const char *format[, address, ...]); 

int swscanf(const wchar_t *buffer, const wchar_t *format[, address, ...]); 

Description 

Scans and formats input from a string.

Note: For details on format specifiers, see scanf.
sscanf scans a series of input fields, one character at a time, reading from a string. Then each field is formatted according to a format specifier passed to sscanf in the format string pointed to by format. Finally, sscanf stores the formatted input at an address passed to it as an argument following format. There must be the same number of format specifiers and addresses as there are input fields. 

sscanf might stop scanning a particular field before it reaches the normal end-of-field (whitespace) character, or it might terminate entirely, for a number of reasons. See scanf for a discussion of possible causes. 

Return Value 

On success, sscanf returns the number of input fields successfully scanned, converted, and stored; the return value does not include scanned fields that were not stored.  

If sscanf attempts to read at end-of-string, it returns EOF. 

On error (If no fields were stored), it returns 0. 

Example  

#include <stdio.h>
#include <stdlib.h>
char *names[4] = {"Peter", "Mike", "Shea", "Jerry"};
#define NUMITEMS 4
int main(void)
{
   int   loop;
   char  temp[4][80];
   char  name[20];
   int   age;
   long  salary;
/* create name, age and salary data */
   for (loop=0; loop < NUMITEMS; ++loop)
      sprintf(temp[loop], "%s %d %ld", names[loop], random(10) + 20, random(5000) + 27500L);
/* print title bar */
   printf("%4s | %-20s | %5s | %15s\n", "#", "Name", "Age", "Salary");
   printf("   --------------------------------------------------\n");
/* input a name, age and salary data */
   for (loop=0; loop < NUMITEMS; ++loop)
      {
      sscanf(temp[loop],"%s %d %ld", &name, &age, &salary);
      printf("%4d | %-20s | %5d | %15ld\n", loop + 1, name, age, salary);
      }
   return 0;
}

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
sscanf 
swscanf 
 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!