Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
scanf %s processing #4594
Original bug ID: 4594
The following program:
let fd = open_in "tst"
when run on the tst file with contents:
produces the following output:
Ocaml versions prior to 3.10 produced the output:
Comment author: @pierreweis
The behaviour you observed is conformant to the specification:
%s: reads a string argument that spreads as much as possible, until the
The first scan returns "xxx" and ends on the space character. The second scan then starts on this space character; according to the specification, it stops immediately and returns an empty string.
If you want to skip the space, you simply have to mention it in the format: if the first format is "%s " then the second scan starts after the space (alternatively, the second scan may be " %s" to skip the space before scanning the second string).
Furthermore, as mentioned in the documentation, the usage of fscanf is kind of picky:
Therefore, I would suggest you avoid using fscanf as much as possible and use bscanf instead (let ib = Scanf.Scanning.from_file "tst" ...).
As you mentioned, Ocaml versions prior to 3.10 produced a different output; in fact, the scanner always skipped blanks after a %s specification; this was confusing in case of reading a file one line at a time, since reading the last field of a line would silently skip any amount of empty lines, thus preventing the detection and proper treatment of empty lines by the programmer.