Borland C++ reference

sscanf

raw OCR

Scans and formats input from a string.

Defined in header <stdio.h>

Syntax

#include <stdio.h>
int sscanf (const char *buffer,const char *format[, address, ...]);

Portability

DOSUNIXWindowsANSI CC++ only
■■■■

Remarks

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

See scant for details

passed to sscanf in the format string pointed to by format. Finally, sscanf

on format specifiers.

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 scant for a discussion of possible causes.

Return value

sscant returns the number of input fields successfully scanned, converted, and stored; the return value does not include scanned fields that were not stored. If no fields were stored, the return value is 0.

If sscant attempts to read at end-of-string, the return value is EOF.

See also

  • tscant
  • scant

Example

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

char *names[4] =  {"Peter", "Mike", "Shea", "Jerry"};

#define NUMITEMS 4

int main (void)
{
   int   loop, age;
   char  temp[4] [80] , naine[20];
   long  salary;

   /* clear the screen  */
   clrscr 0;

   /* create name, age and  salary data */
   for  (loop=0; loop < NUMITEMS; ++loop)
      sprintf (temp[loop], "%s %d Ud",  names[loop],
              random (10) + 20,
              random(5000) + 27500L
              );
   /* print title bar */
   printf("%4s  I l-20s t %5s I %15s\n",
          "#", "Name",  "Age", "Salary");
   printf("                            "

   /* input a name, age and  salary data */
   for (loop=0; loop < NUMITEMS;  ++loop) {
      sscanf (temp[loop],"%s %d %ld", &name, &age, *salary);
      printf("%4d  I %-20s I %5d ! %151d\n",
             loop + 1, name, age,  salary);
      }
   return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.