lua-users home
lua-l archive

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

Am 05.09.2017 um 15:13 schrieb Shmuel Zeigerman:
I wonder why the first read from an empty file returns nil rather than an empty string.
E.g. fp:read(10) --> nil

If the zero-sized file exists then it can be interpreted as a file containing an empty string (IMHO).

If you have a single \n in your file. the first fp:read("*l") will return "", the second fp:read("*l") will return nil.
f you read from a file that contains some characters but no \n, you get nil.
So you get a non-nil for all \n-terminated lines.
and a nil for the rest, even if it contains some characters.

To me this is consistent
You may need to call fp:read("*a) if you get nil as result to get a final line without \n (which may exist in some text files)