lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> In a recent comments exchange on Stack Overflow [1] an issue with the
> use of io.read("*n") came up which is related also to this [2] lua-l post.
> 
> The problem is that when reading a number fails, the manual is not clear
> about what happens to the underlying stream, i.e. that an unpredictable
> number of characters may be consumed (even no characters). [...]
> 
> "Reads the file file, according to the given formats, which specify what
> to read. For each format, the function returns a string (or a number)
> with the characters read, or nil if it cannot read data with the
> specified format. When called without formats, it uses a default format
> that reads the next line (see below). _When using the "*n" format
> and the read operation fails, an unspecified number (>= 0) of characters
> from file may be consumed._"

(The original message above is from September; it was on my list of
things to answer; sorry for the delay...)

The problem is that the specification is not very clear, and in my
understanding the implementation in libc is buggy... For instance,
consider this example (in Linux):

> print(io.read('*n', '*l'))
3.4e-                                 <<< input
3.4	                              <<< output

What happened with "e-"? Did fscanf accepted "3.4e-" as a number,
or it simply thrown away the "e-"? In both cases, it seems the wrong
behavior. How to document it? (It seems like the read operation
consumed "an unspecified number of characters" even though the
operation did not fail...)

-- Roberto