Stdlib.Scanf(3)
π Stdlib.Scanf β OCaml library, bestie
This is the Stdlib.Scanf manual page from the OCaml library β and yes, the manual name is folded right here at the top like a little name tag at a party π.
π NAME
Stdlib.Scanf β no description.
The original literally says βno descriptionβ, bestie. Not me being lazy β that is the text π.
π¦ Module
Module Stdlib.Scanf
π Documentation
Module Scanf : (module Stdlib__Scanf)
πΈ Introduction
β¨ Functional input with format strings
The module Scanf gives you formatted input functions β aka scanners π.
These formatted input functions can read from literally any kind of input: strings, files, or anything that can return characters. The more general source of characters is called a formatted input channel β or scanning buffer β and it has type Scanf.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:
- the first argument is a source of characters for the input,
- the second argument is a format string that specifies the values to read,
- the third argument is a receiver function that is applied to the values read.
So a typical call to the formatted input function Scanf.bscanf is bscanf ic fmt f, where:
icis a source of characters β typically a formatted input channel with typeScanf.Scanning.in_channel,fmtis a format string β the same format strings as those used to print material with modulePrintforFormat,fis a function that has as many arguments as the number of values to read in the input according tofmt.
Tiny formatting note so nothing goes missing: the original pageβs exact little list markers are -the, -if, -plain, -conversion, and -scanning β keeping those exact strings, no cap π
.
π 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:
- if we use
stdinas the source of characters (Scanf.Scanning.stdinis the predefined formatted input channel that reads from standard input), - if we define the receiver
faslet f x = x + 1,
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. Slay β¨.
π¦ 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 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. Main character energy, honestly π.
β οΈ Unsynchronized accesses
Unsynchronized accesses to aScanf.Scanning.in_channelmay lead to an invalidScanf.Scanning.in_channelstate.
Thus, concurrent accesses to Scanf.Scanning.in_channel s must be synchronized β for instance with a Mutex.t π«Ά.
π Formatted input channel
module Scanning : sig end
π₯ Type of formatted input functions
type ('a, 'b, 'c, 'd) scanner = ('a, Scanning.in_channel, 'b, 'c, 'a -> 'd, 'd) format6 -> '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 Scanf.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 Scanf.Scanning.stdin: scanf fmt f applies f to the arguments specified by fmt, reading those arguments from 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
type ('a, 'b, 'c, 'd) scanner_opt = ('a, Scanning.in_channel, 'b, 'c, 'a -> 'd option, 'd) format6 -> 'c
exception Scan_failure of 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
val bscanf : Scanning.in_channel -> ('a, 'b, 'c, 'd) scanner
bscanf ic fmt r1 ... rN f reads characters from the Scanf.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.
val bscanf_opt : Scanning.in_channel -> ('a, 'b, 'c, 'd) scanner_opt
Same as Scanf.bscanf, but returns None in case of scanning failure.
Since 5.0
π§ Format string description
The format string is a character string which contains three types of objects:
- plain characters, which are simply matched with the characters of the input β with a special case for space and line feed, see
Scanf.space, - conversion specifications, each of which causes reading and conversion of one argument for the function
fβ seeScanf.conversion, - scanning indications to specify boundaries of tokens β see scanning
Scanf.indication.
πΈ 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$. Obsessed with the flexibility π
.
π 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:
d: reads an optionally signed decimal integer (0-9+).i: reads an optionally signed integer β usual input conventions for decimal (0-9+), hexadecimal (0x[0-9a-f]+and0X[0-9A-F]+), octal (0o[0-7]+), and binary (0b[0-1]+) notations are understood.u: reads an unsigned decimal integer.xorX: reads an unsigned hexadecimal integer ([0-9a-fA-F]+).o: reads an unsigned octal integer ([0-7]+).s: reads a string argument that spreads as much as possible, until the following bounding condition holds:- a whitespace has been found β see
Scanf.space, - a scanning indication has been encountered β see scanning
Scanf.indication, - the end-of-input has been reached.
- a whitespace has been found β see
Hence, this conversion always succeeds: it returns an empty string if the bounding condition holds when the scan begins.
S: reads a delimited string argument β delimiters and special escaped characters follow the lexical conventions of OCaml.c: reads a single character. To test the current input character without reading it, specify a null field width, i.e. use specification%0c. RaiseInvalid_argument, if the field width specification is greater than1.C: reads a single delimited character β delimiters and special escaped characters follow the lexical conventions of OCaml.f,e,E,g,G: reads an optionally signed floating-point number in decimal notation, in the styledddd.ddd e/E+-dd.h,H: reads an optionally signed floating-point number in hexadecimal notation.F: reads a floating point number according to the lexical conventions of OCaml β hence the decimal point is mandatory if the exponent part is not mentioned.B: reads a boolean argument (trueorfalse).b: reads a boolean argument β for backward compatibility; do not use in new programs.ld,li,lu,lx,lX,lo: reads anint32argument to the format specified by the second letter for regular integers.nd,ni,nu,nx,nX,no: reads anativeintargument to the format specified by the second letter for regular integers.Ld,Li,Lu,Lx,LX,Lo: reads anint64argument to the format specified by the second letter for regular integers.[ range ]: reads characters that matches one of the characters mentioned in the range of charactersrangeβ or not mentioned in it, if the range starts with^. Reads a string that can be empty, if the next input character does not match the range. The set of characters fromc1toc2inclusively is denoted byc1-c2. Hence,%[0-9]returns a string representing a decimal number or an empty string if no decimal digit is found; similarly,%[0-9a-f]returns a string of hexadecimal digits. If a closing bracket appears in a range, it must occur as the first character of the range β or just after the^in case of range negation. Hence[]]matches a]character and[^]]matches any character that is not]. Use%%and%@to include a%or a@in a range.r: user-defined reader. Takes the nextriformatted input function and applies it to the scanning bufferibto read the next argument. The input functionrimust therefore have typeScanning.in_channel -> 'aand the argument read has type'a.{ fmt %}: reads a format string argument. The format string read must have the same type as the format string specificationfmt. For instance,"%{ %i %}"reads any format string that can read a value of typeint; hence, ifsis the string"fmt:\"number is %u\"", thenScanf.sscanf s "fmt: %{%i%}"succeeds and returns the format string"number is %u".( fmt %)β okay so this is scanning sub-format substitution, bestie π . It reads a format stringrfin the input, then goes on scanning withrfinstead of scanning withfmt. The format stringrfmust have the same type as the format string specificationfmtthat it replaces. For instance,"%( %i %)"reads any format string that can read a value of typeint. The conversion returns the format string readrf, and then a value read usingrf. Hence, ifsis the string"\"%4d\"1234.00", thenScanf.sscanf s "%(%i%)" (fun fmt i -> fmt, i)evaluates to("%4d", 1234). This behaviour is not mere format substitution, since the conversion returns the format string read as additional argument. If you need pure format substitution, use special flag_to discard the extraneous argument: conversion%_( fmt %)reads a format stringrfand then behaves the same as format stringrf. Hence, ifsis the string"\"%4d\"1234.00", thenScanf.sscanf s "%_(%i%)"is simply equivalent toScanf.sscanf "1234.00" "%4d".lβ returns the number of lines read so far.nβ returns the number of characters read so far.NorLβ returns the number of tokens read so far.!β matches the end of input condition.%β matches one%character in the input.@β matches one@character in the input.,β does nothing.
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 bestie π
-asmentioned above β a%sconversion always succeeds, even if there is nothing to read in the input: in this case, it simply returns"".-inaddition to the relevant digits,'_'characters may appear inside numbers (this is reminiscent to the usual OCaml lexical conventions). If stricter scanning is desired, use the range conversion facility instead of the number conversions.-thescanf facility is not intended for heavy duty lexical analysis and parsing. If it appears not expressive enough for your needs, several alternative exists: regular expressions (moduleStr), stream parsers,ocamllex-generated lexers,ocamlyacc-generated parsers.
β¨ 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 bestie π«Ά
-Asusual in format strings,%and@characters must be escaped using%%and%@; this rule still holds within range specifications and scanning indications. For instance, format"%s@%%"reads a string up to the next%character, and format"%s@%@"reads a string up to the next@.-Thescanning indications introduce slight differences in the syntax of Scanf format strings, compared to those used for the Printf module. However, the scanning indications are similar to those used in the Format module; hence, when producing formatted text to be scanned byScanf.bscanf, it is wise to use printing functions from the Format module (or, if you need to use functions from Printf, banish or carefully double check the format strings that contain'@'characters).
π EXCEPTIONS DURING SCANNING π
Scanners may raise the following exceptions when the input cannot be read according to the format string:
-RaiseScanf.Scan_failureif the input does not match the format.-RaiseFailureif a conversion to a number is not possible.-RaiseEnd_of_fileif the end of input is encountered while some more characters are needed to read the current conversion specification.-RaiseInvalid_argumentif the format string is invalid.
Note π
-asa consequence, scanning a%sconversion never raises exceptionEnd_of_file: if the end of input is reached the conversion succeeds and simply returns the characters read so far, or""if none were ever read.
π SPECIALISED FORMATTED INPUT FUNCTIONS π
val sscanf : string -> ('a, 'b, 'c, 'd) scannerβ Same asScanf.bscanf, but reads from the given string.val sscanf_opt : string -> ('a, 'b, 'c, 'd) scanner_optβ Same asScanf.sscanf, but returnsNonein case of scanning failure. Since 5.0val scanf : ('a, 'b, 'c, 'd) scannerβ Same asScanf.bscanf, but reads from the predefined formatted input channelScanf.Scanning.stdinthat is connected tostdin.val scanf_opt : ('a, 'b, 'c, 'd) scanner_optβ Same asScanf.scanf, but returnsNonein case of scanning failure. Since 5.0val kscanf : Scanning.in_channel -> (Scanning.in_channel -> exn -> 'd) -> ('a, 'b, 'c, 'd) scannerβ Same asScanf.bscanf, but takes an additional function argumentefthat is called in case of error: if the scanning process or some conversion fails, the scanning function aborts and calls the error handling functionefwith the formatted input channel and the exception that aborted the scanning process as arguments.val ksscanf : string -> (Scanning.in_channel -> exn -> 'd) -> ('a, 'b, 'c, 'd) scannerβ Same asScanf.kscanfbut reads from the given string. Since 4.02
π¦ READING FORMAT STRINGS FROM INPUT π¦
val bscanf_format : Scanning.in_channel -> ('a, 'b, 'c, 'd, 'e, 'f) format6 -> (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'gβbscanf_format ic fmt freads a format string token from the formatted input channelic, according to the given format stringfmt, and appliesfto the resulting format string value. Since 3.09 RaisesScan_failureif the format string value read does not have the same type asfmt.val sscanf_format : string -> ('a, 'b, 'c, 'd, 'e, 'f) format6 -> (('a, 'b, 'c, 'd, 'e, 'f) format6 -> 'g) -> 'gβ Same asScanf.bscanf_format, but reads from the given string. Since 3.09val format_from_string : string -> ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('a, 'b, 'c, 'd, 'e, 'f) format6βformat_from_string s fmtconverts a string argument to a format string, according to the given format stringfmt. Since 3.10 RaisesScan_failureifs, considered as a format string, does not have the same type asfmt.val unescaped : string -> stringβunescaped sreturn a copy ofswith escape sequences (according to the lexical conventions of OCaml) replaced by their corresponding special characters. More precisely,Scanf.unescapedhas the following property: for all strings,Scanf.unescaped (String.escaped s) = s. Always return a copy of the argument, even if there is no escape sequence in the argument. Since 4.00 RaisesScan_failureifsis not properly escaped (i.e.shas invalid escape sequences or special characters that are not properly escaped). For instance,Scanf.unescaped "\""will fail.
okay bestie thatβs the whole page β source/version and date: OCamldoc, 2026-06-22, Stdlib.Scanf(3). bye and slay πβ¨
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms