sscanf -- Read Data from Buffer

Format

#include <stdio.h>
int sscanf(const char *buffer, const char *format, argument-list);

Language Level: ANSI, POSIX, XPG4, Extension
sscanf reads data from buffer into the locations given by argument-list. Each argument must be a pointer to a variable with a type that corresponds to a type specifier in the format-string. See scanf -- Read Data for a description of the format-string.

In extended mode, on OS/2 and Windows, sscanf also reads in the strings "INFINITY", "INF", and "NAN" (in upper or lowercase) and converts them to the corresponding floating-point value. The sign of the value is determined by the format specification.

Return Value
sscanf returns the number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned.

The return value is EOF when the end of the string is encountered before anything is converted.

Example
This example uses sscanf to read various data from the string tokenstring, and then displays that data.

#include <stdio.h>
#include <wchar.h>
int main(void)
{
   char *tokenstring = "15 12 14";
   wchar_t *widestring = (wchar_t *)"ABC Z";
   wchar_t ws[81]
   wchar_t wc;
   int i;
   float fp;
   char s[SIZE];
   char c;
   /* Input various data                              */
   /* In the first invocation of sscanf, the format   */
   /* string is "%s %c%d%f". If there were no space   */
   /* between %s and %c, sscanf would read the first  */
   /* character following the string, which is a      */
   /* space.                                          */
   sscanf(tokenstring, "%s %c%d%f", s, &c, &i, &fp);
   sscanf(widestring, "%S %C", ws, &wc);
   /* Display the data */
   printf("\nstring = %s\n",s);
   printf("character = %c\n",c);
   printf("integer = %d\n",i);
   printf("floating-point number = %f\n",fp);
   printf("wide-character string = %S\n",ws);
   printf("wide-character = %C\n",wc);
   return 0;
   /****************************************************
      The output should be:
      string = 15
      character = 1
      integer = 2
      floating-point variable = 14.000000
      wide-character string = ABC
      wide-character = Z
   ****************************************************/
}


_cscanf -- Read Data From Keyboard
fscanf -- Read Data from a Stream
scanf -- Read Data
sprintf -- Print Formatted Data to Buffer
swscanf -- Read Wide-Character Data from Buffer
<stdio.h>