lua-users home
lua-l archive

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


> The killer is loss of data.
> 
> If you modify astring to 99.eastring it fatally falls over. Returns
> nothing but then a read of string returns astring, the 99. has been lost.

It works fine for me in Linux, reading files with both \r\n and \n as line
terminators. However, the behaviour does seem to depend on the details of
how fscanf is implemented because in Mac OS X I get a different result.
The issue of course is whether fscanf considers "99.e" a valid number.

In Linux:
---	in
40	number
99	number
["astring\r"]	string	8
---	in2
40	number
99	number
["astring"]	string	7

In Mac OS X:
---	in
40	number
99	number
["eastring\r"]	string	9
---	in2
40	number
99	number
["eastring"]	string	8

Here is a complete session, with source code and data:

+ xxd in
0000000: 3430 0d0a 3939 2e65 6173 7472 696e 670d  40..99.eastring.
0000010: 0a                                       .
+ xxd in2
0000000: 3430 0a39 392e 6561 7374 7269 6e67 0a    40.99.eastring.
+ cat b
function test(a)
	print("---",a)
	f=assert(io.open(a))
	x=f:read("*n")
	print(x,type(x))
	x=f:read("*n")
	print(x,type(x))
	x=f:read("*l")
	print(string.format("[%q]",x),type(x),#x)
	f:close()
end
test"in"
test"in2"
+ lua b
---	in
40	number
99	number
["astring\r"]	string	8
---	in2
40	number
99	number
["astring"]	string	7