lua-users home
lua-l archive

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


On Tue, Sep 5, 2017 at 3:13 PM, Shmuel Zeigerman <shmuz@013net.net> wrote:
> 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).

It can, but "number: reads a string with up to this number of bytes,
returning nil on end of file. If number is zero, it reads nothing and
returns an empty string, or nil on end of file. "

It is positioned at end of file, so it returns nil. Note it does not
distinguish between first or subsequent reads, soif it returned the
empty string it will enter an inifnite loop at EOF.

Normally, when you want to be able to have  an empty string, you use
line separators, or other similar format for your datafiles, so i.e.,
"\n" is a file with just an empty string but "" is a file with no
strings on it.

Francisco Olarte.