The only documented formats are:
- no format: See
"*l"
- 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.
"*n": reads a number; this is the only format that returns a number instead of a string.
"*a": reads the whole file, starting at the current position. On end of file, it returns the empty string.
"*l": reads the next line skipping the end of line, returning nil on end of file. This is the default format.
"*L": reads the next line keeping the end of line (if present), returning nil on end of file.
Looking at the sources (liolib.c, 424ff.), the implementation is much more accepting:
- Checks whether there is an argument and otherwise reads a line.
- Checks for a value of type "number", in which case it reads the indicated number of bytes.
- Converts the argument to a string.
- Compares the first two characters with the documented formats, without checking the string's size.
The last point is not a bug for strings with less than two characters (excluding possibly the empty string), because Lua-strings have a 0-terminator for interoperability that is not part of the length.
Taking a look at the "*n"-format, that's the code for it:
static int read_number (lua_State *L, FILE *f) {
lua_Number d;
if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
lua_pushnumber(L, d);
return 1;
}
else {
lua_pushnil(L); /* "result" to be removed */
return 0; /* read fails */
}
}
- Try to read a number with
fscanf using format LUA_NUMBER_SCAN (configurable, standard is "%lf", see luaconf.h).
- Return the number or
nil on failure.