OCaml 使用 Scanf 读取一个字符

OCaml Reading a char with Scanf

有什么方法可以用 Scan f 读取字符吗?我不想读一行,因为我正在读其他类型的变量。

4 个 ... 223 5 72

由于行的结构是不规则的,我只想担心读取此示例中的字符 'a'。 假设我已经读取了int,只需要读取char。

使用 Scanf 模块:首先打开扫描缓冲区,然后将 bscanf 与正确的扫描仪一起使用。

假设您的文件包含: 4个45

open Scanf;;
let b = Scanning.from_file "<your file>";;
let a = bscanf b "%d" (fun x -> x);; will return 4
let a = bscanf b "%c" (fun x -> x);; will return ' '
let a = bscanf b "%c" (fun x -> x);; will return 'a'
let a = bscanf b "%c" (fun x -> x);; will return ' '
let a = bscanf b "%d" (fun x -> x);; will return 45

在 Scanf 的文档中,建议使用 bscan 而不是 fscanf。