Module Stdlib.Scanf

Formatted input functions.

Introduction

Functional input with format strings

The module Scanf provides formatted input functions or scanners.

The formatted input functions can read from any kind of input, including strings, files, or anything that can return characters. The more general source of characters is named a formatted input channel (or scanning buffer) and has type Scanning.in_channel. The more general formatted input function reads from any scanning buffer and is named bscanf.

Generally speaking, the formatted input functions have 3 arguments:

Hence, a typical call to the formatted input function Scanf.bscanf is bscanf ic fmt f, where:

A simple example

As suggested above, the expression bscanf ic "%d" f reads a decimal integer n from the source of characters ic and returns f n.

For instance,

then bscanf Scanning.stdin "%d" f reads an integer n from the standard input and returns f n (that is n + 1). Thus, if we evaluate bscanf stdin "%d" f, and then enter 41 at the keyboard, the result we get is 42.

Formatted input as a functional feature

The OCaml scanning facility is reminiscent of the corresponding C feature. However, it is also largely different, simpler, and yet more powerful: the formatted input functions are higher-order functionals and the parameter passing mechanism is just the regular function application not the variable assignment based mechanism which is typical for formatted input in imperative languages; the OCaml format strings also feature useful additions to easily define complex tokens; as expected within a functional programming language, the formatted input functions also support polymorphism, in particular arbitrary interaction with polymorphic user-defined scanners. Furthermore, the OCaml formatted input facility is fully type-checked at compile time.

Formatted input channel

module Scanning: { ... };

Type of formatted input functions

type scanner('a, 'b, 'c, 'd) = format6('a, Scanning.in_channel, 'b, 'c, 'a => 'd, 'd) => 'c;

The type of formatted input scanners: ('a, 'b, 'c, 'd) scanner is the type of a formatted input function that reads from some formatted input channel according to some format string; more precisely, if scan is some formatted input function, then scan ic fmt f applies f to all the arguments specified by format string fmt, when scan has read those arguments from the Scanning.in_channel formatted input channel ic.

For instance, the Scanf.scanf function below has type ('a, 'b, 'c, 'd) scanner, since it is a formatted input function that reads from Scanning.stdin: scanf fmt f applies f to the arguments specified by fmt, reading those arguments from Stdlib.stdin as expected.

If the format fmt has some %r indications, the corresponding formatted input functions must be provided before receiver function f. For instance, if read_elem is an input function for values of type t, then bscanf ic "%r;" read_elem f reads a value v of type t followed by a ';' character, and returns f v.

  • since 3.10.0
exception Scan_failure(string);

When the input can not be read according to the format string specification, formatted input functions typically raise exception Scan_failure.

The general formatted input function

let bscanf: Scanning.in_channel => scanner('a, 'b, 'c, 'd);

bscanf ic fmt r1 ... rN f reads characters from the Scanning.in_channel formatted input channel ic and converts them to values according to format string fmt. As a final step, receiver function f is applied to the values read and gives the result of the bscanf call.

For instance, if f is the function fun s i -> i + 1, then Scanf.sscanf "x= 1" "%s = %i" f returns 2.

Arguments r1 to rN are user-defined input functions that read the argument corresponding to the %r conversions specified in the format string.

Format string description

The format string is a character string which contains three types of objects:

The space character in format strings

As mentioned above, a plain character in the format string is just matched with the next character of the input; however, two characters are special exceptions to this rule: the space character (' ' or ASCII code 32) and the line feed character ('\n' or ASCII code 10). A space does not match a single space character, but any amount of 'whitespace' in the input. More precisely, a space inside the format string matches any number of tab, space, line feed and carriage return characters. Similarly, a line feed character in the format string matches either a single line feed or a carriage return followed by a line feed.

Matching any amount of whitespace, a space in the format string also matches no amount of whitespace at all; hence, the call bscanf ib "Price = %d $" (fun p -> p) succeeds and returns 1 when reading an input with various whitespace in it, such as Price = 1 $, Price = 1 $, or even Price=1$.

Conversion specifications in format strings

Conversion specifications consist in the % character, followed by an optional flag, an optional field width, and followed by one or two conversion characters.

The conversion characters and their meanings are:

Following the % character that introduces a conversion, there may be the special flag _: the conversion that follows occurs as usual, but the resulting value is discarded. For instance, if f is the function fun i -> i + 1, and s is the string "x = 1", then Scanf.sscanf s "%_s = %i" f returns 2.

The field width is composed of an optional integer literal indicating the maximal width of the token to read. For instance, %6d reads an integer, having at most 6 decimal digits; %4f reads a float with at most 4 characters; and %8[\000-\255] returns the next 8 characters (or all the characters still available, if fewer than 8 characters are available in the input).

Notes:

Scanning indications in format strings

Scanning indications appear just after the string conversions %s and %[ range ] to delimit the end of the token. A scanning indication is introduced by a @ character, followed by some plain character c. It means that the string token should end just before the next matching c (which is skipped). If no c character is encountered, the string token spreads as much as possible. For instance, "%s@\t" reads a string up to the next tab character or to the end of input. If a @ character appears anywhere else in the format string, it is treated as a plain character.

Note:

Exceptions during scanning

Scanners may raise the following exceptions when the input cannot be read according to the format string:

Note:

Specialised formatted input functions

let sscanf: string => scanner('a, 'b, 'c, 'd);

Same as Scanf.bscanf, but reads from the given string.

let scanf: scanner('a, 'b, 'c, 'd);

Same as Scanf.bscanf, but reads from the predefined formatted input channel Scanf.Scanning.stdin that is connected to Stdlib.stdin.

let kscanf: Scanning.in_channel => (Scanning.in_channel => exn => 'd) => scanner('a, 'b, 'c, 'd);

Same as Scanf.bscanf, but takes an additional function argument ef that is called in case of error: if the scanning process or some conversion fails, the scanning function aborts and calls the error handling function ef with the formatted input channel and the exception that aborted the scanning process as arguments.

let ksscanf: string => (Scanning.in_channel => exn => 'd) => scanner('a, 'b, 'c, 'd);

Same as Scanf.kscanf but reads from the given string.

  • since 4.02.0

Reading format strings from input

let bscanf_format: Scanning.in_channel => format6('a, 'b, 'c, 'd, 'e, 'f) => (format6('a, 'b, 'c, 'd, 'e, 'f) => 'g) => 'g;

bscanf_format ic fmt f reads a format string token from the formatted input channel ic, according to the given format string fmt, and applies f to the resulting format string value.

  • raises Scan_failure

    if the format string value read does not have the same type as fmt.

  • since 3.09.0
let sscanf_format: string => format6('a, 'b, 'c, 'd, 'e, 'f) => (format6('a, 'b, 'c, 'd, 'e, 'f) => 'g) => 'g;

Same as Scanf.bscanf_format, but reads from the given string.

  • since 3.09.0
let format_from_string: string => format6('a, 'b, 'c, 'd, 'e, 'f) => format6('a, 'b, 'c, 'd, 'e, 'f);

format_from_string s fmt converts a string argument to a format string, according to the given format string fmt.

  • raises Scan_failure

    if s, considered as a format string, does not have the same type as fmt.

  • since 3.10.0
let unescaped: string => string;

unescaped s return a copy of s with escape sequences (according to the lexical conventions of OCaml) replaced by their corresponding special characters. More precisely, Scanf.unescaped has the following property: for all string s, Scanf.unescaped (String.escaped s) = s.

Always return a copy of the argument, even if there is no escape sequence in the argument.

  • raises Scan_failure

    if s is not properly escaped (i.e. s has invalid escape sequences or special characters that are not properly escaped). For instance, Scanf.unescaped "\"" will fail.

  • since 4.00.0

Deprecated

let fscanf: in_channel => scanner('a, 'b, 'c, 'd);
  • deprecated

    Scanf.fscanf is error prone and deprecated since 4.03.0.

    This function violates the following invariant of the Scanf module: To preserve scanning semantics, all scanning functions defined in Scanf must read from a user defined Scanning.in_channel formatted input channel.

    If you need to read from a Stdlib.in_channel input channel ic, simply define a Scanning.in_channel formatted input channel as in let ib = Scanning.from_channel ic, then use Scanf.bscanf ib as usual.

let kfscanf: in_channel => (Scanning.in_channel => exn => 'd) => scanner('a, 'b, 'c, 'd);
  • deprecated

    Scanf.kfscanf is error prone and deprecated since 4.03.0.