sscanf()

Reads data from "s" and stores the data according to the "format" parameter into the additional arguments.

Synopsis

intsscanf(strings,stringformat,<type1>&var1 [,<type2>&var2 ...]);

Parameters

Parameter Description
s String to be read
format

Format to be read. Format specifies how the parameter "s" is interpreted:

%[*][width][length]specifier

For details see chapter sprintf().

var1, var2, ... Target variables

Errors

missing/incorrect arguments

Description

Reads under the specified format from the stringsand stores the converted values with the help of the following arguments. sscanf() returns the number of converted and stored entries, in the event of errors or if the end of the input string is reached prior to the first conversion, EOF. Otherwise, sscanf() functions like fscanf(). For more examples and description of the format signs see chaptersprintf().

sscanf() only functions with strings up to a maximum size of 499 characters. Otherwise the function stops scanning and returns the number of already successfully scanned target variables.

For invalid format specifiers e.g. %i, the scanning process stops and the function returns the number of successfully scanned target variables.

sscanf() works byte-oriented and independent of the coding type of the used characters. For example, when processing a string using UTF-8 coding in the format of "%10s" into Cyrillic characters (one character is coded with 2 Bytes), then a string with 5 Cyrillic characters (each being 2 Bytes long) is the result.

Example

main(mapping event)
{
  string text;
  int number = 123;
  int rc = sprintf(text, "%d", number);
  DebugN("rc", rc); // rc will be 3, since three characters were printed
  DebugN("text", text); // "123"
  number = 999; // init with some value to see that sscanf() has changed the value
  rc = sscanf(text, "%d", number); // scans "123" to number (123)
  DebugN("rc", rc); // 1, since one variable (number) was scanned
  DebugN("number", number); // 123
}

Writes the value of a float variable to a string variable s.

main(mapping event)
{
  string text;
  float number = 2.33;
  int rc = sprintf(text, "%5.3f", number);
  DebugN("rc", rc); // rc will be 5, since five chars were printed ("2.330")
  DebugN("text", text); // "2.330"
  rc = sscanf(text, "%f", number);
  DebugN("rc", rc); // rc will be 1, sonce one value (parameter number) was scanned
  DebugN("number", number); // 2.33
}

The sscanf() is used like sprintf(). For more different examples of sscanf(), see chapter sprintf().

Assignment

Strings

Availability

UI, CTRL