Module Scanf

module Scanf: sig .. end
Formatted input functions.

module Scanning: sig .. end
Scanning buffers.
exception Scan_failure of string
The exception that formatted input functions raise when the input cannot be read according to the given format.
val bscanf : Scanning.scanbuf ->
('a, Scanning.scanbuf, 'b) format -> 'a -> 'b
bscanf ib format f reads tokens from the scanning buffer ib according to the format string format, converts these tokens to values, and applies the function f to these values. The result of this application of f is the result of the whole construct.

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

Raise Scanf.Scan_failure if the given input does not match the format.

Raise Failure if a conversion to a number is not possible.

Raise End_of_file if the end of input is encountered while scanning and the input matches the given format so far.

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

Among plain characters the space character (ASCII code 32) has a special meaning: it matches ``whitespace'', that is any number of tab, space, newline and carriage return characters. Hence, a space in the format matches any amount of whitespace in the input.

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 introducing a conversion, there may be the special flag _: the conversion that follows occurs as usual, but the resulting value is discarded.

The field widths are 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; and %4f reads a float with at most 4 characters.

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 constant 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 tabulation character. If a scanning indication @c does not follow a string conversion, it is ignored and treated as a plain c character.


val fscanf : in_channel ->
('a, Scanning.scanbuf, 'b) format -> 'a -> 'b
Same as Scanf.bscanf, but inputs from the given channel.

Warning: since all scanning functions operate from a scanning buffer, be aware that each fscanf invocation must allocate a new fresh scanning buffer (unless careful use of partial evaluation in the program). Hence, there are chances that some characters seem to be skipped (in fact they are pending in the previously used buffer). This happens in particular when calling fscanf again after a scan involving a format that necessitates some look ahead (such as a format that ends by skipping whitespace in the input).

To avoid confusion, consider using bscanf with an explicitly created scanning buffer. Use for instance Scanning.from_file f to allocate the scanning buffer reading from file f.

This method is not only clearer it is also faster, since scanning buffers to files are optimized for fast bufferized reading.

val sscanf : string -> ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b
Same as Scanf.bscanf, but inputs from the given string.
val scanf : ('a, Scanning.scanbuf, 'b) format -> 'a -> 'b
Same as Scanf.bscanf, but reads from the predefined scanning buffer Scanf.Scanning.stdib that is connected to stdin.
val kscanf : Scanning.scanbuf ->
(Scanning.scanbuf -> exn -> 'a) ->
('b, Scanning.scanbuf, 'a) format -> 'b -> 'a
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 applies the error handling function ef to the scanning buffer and the exception that aborted the scanning process.