lua-users home
lua-l archive

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


Hi,

I am having a problem when program that use read() or read("*l") on files
that contain arbitrary binary data.  I would expect that it would always
read up to a "\n" and not care what is in the file or how long the line
is, but it appears to be broken, or at least not behaving the way I
expect...  I have tested it on both Windows and Linux, so it isn't an
environmental quirk.  Here is a program that demonstrates the
problem.  The read() seems to only go halfway through the file...

hdl=openfile("data","wb")
for x=1,1000000
do
	write(hdl,(strchar(random(0,255))))
end
closefile(hdl)
lines=0 bytes=0
hdl=openfile("data","rb")
while 1
do
	b=read(hdl)
	if not b
	then
		break
	end
	lines=lines+1
	bytes=bytes+strlen(b)+1 -- 1 extra for \n
end
closefile(hdl)
print("read *l got:")
print("bytes:", bytes)
print("lines:", lines)
print()
lines=0 bytes=0
hdl=openfile("data", "rb")
while 1
do
	b=read(hdl,1)
	if not b
	then
		break
	end
	bytes=bytes+1
	if b=="\n"
	then
		lines=lines+1
	end
end
print("read N got:")
print("bytes:", bytes)
print("lines:", lines)
print()