[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Reading from an empty file
- From: Oliver Kroth <oliver.kroth@...>
- Date: Wed, 6 Sep 2017 06:29:28 +0200
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.
If you have a single \n in your file. the first fp:read("*l") will
return "", the second fp:read("*l") will return nil.
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).
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)