Unit 'sysutils' Package
[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] [#rtl]

SScanf

Scan a string for substrings and return the content of these substrings as typed values.

Declaration

Source position: sysstrh.inc line 278

function SScanf(

  const s: string;

  const fmt: string;

  const Pointers: array of Pointer

):SizeInt;

Description

SScanF does in essence the opposite of Format: it scans the string S for the elements specified in Fmt, and returns the value of the found elements in the memory locations pointed to by the addresses in Pointers. The Fmt can contain placeholders of the form %X where X can be one of the following characters:

d
Placeholder for a decimal number.
u
Placeholder for an unsigned decimal number.
f
Placeholder for a floating point number (an extended)
s
Placeholder for a string of arbitrary length, up to the next space.
c
Placeholder for a single character

%% matches a single % character in S. A space in Fmt matches any number of spaces in S, including none. Any other character in Fmt must match the character at the current position in S: on a mismatch, scanning stops.

The Pointers array contains a list of pointers, each pointer should point to a memory location of a type that corresponds to the type of placeholder in that position:

d
A pointer to an integer.
u
A pointer to a cardinal.
f
A pointer to an extended.
s
A pointer to an ansistring.
c
A pointer to a single character.

On return, these locations will be filled with the actual values found in S for the placeholders in fmt.

The return value of the function is the number of values that were converted and assigned. Scanning stops at the first placeholder that cannot be converted or at the first literal character that does not match, so the result is less than the number of placeholders if S does not match Fmt completely. Literal characters after the last placeholder are not checked: SScanf('(1, 2', '(%d, %d)', [@a,@b]) returns 2. Placeholders that were not converted leave their memory location unchanged.

Errors

No error checking is performed on the type of the memory location. An EFormatError exception is raised if Fmt contains an unknown placeholder. An EConvertError exception is raised if a number is out of range for its type.

See also

Format

  

Format a string with given arguments.

Example

Program Example98;
{$mode objfpc}
{$h+}
{ This program demonstrates the  function }

Uses sysutils;

var
  count,i: Integer;
  f: Extended;
  s: String;
  
begin
  count:=SScanf('234 32.4 hello','%d %f %s',[@i,@f,@s]);
  writeln(count,' ',i,' ',f,' ',s);
End.

Documentation generated on: Oct 31 2026 Report a problem on this page