[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Reading a zero-terminated string
- From: Enrico Colombini <erix@...>
- Date: Mon, 09 Jun 2008 14:09:33 +0200
I have to read 0-terminated 8-bit strings from a file (actually, a
'binary' file under Windows).
I tried f:read('*l') and it worked fine... but then I noticed that its
behavior with \0 is undocumented, so I had a look at liolib.c, saw that
it uses fgets... and realized that fgets's behavior with \0 seems to be
undocumented too.
So I basically see three choices:
1) use the undocumented behavior of f:read/fgets.
2) scan char-by-char to find the \0, use f:seek to move back to the
starting point and read the string with f:read(n).
3) read a byte at a time into a table and then use table.concat.
I think 2 and 3 are both inefficient in different ways (though I guess 3
is probably much worse), but 1 is risky being undocumented.
Using a fixed-size buffer block could possibly be more efficient but
looks like overkill.
Did I miss a simpler and/or cleaner (pure-Lua) way to read a
0-terminated string into a Lua string?
Enrico