I want to open a file and loop through each line versus dumping the contents of the file to a buffer. Anyone have sample code?
I think you could do this with a regular expression and the string.gmatch() function. Try this out:
local buffer, bytes = file.read(filename)
for line in string.gmatch(buffer, "[^\r\n]+") do
print("line = "..line)
I tested this on Linux and it worked. I think the regular expression should ignore all of the Windows line-ennding characters as well, but you should probably verify if that is where you are running it.
The file.read() method reads the whole file into a large buffer no matter how you process the buffer afterwards.
The snipped below uses the Lua io library to read a line at a time, thus allocating very little memory.
f = assert(io.open("nas.log","r"))for line in f:lines() do printf("-> %s",line)endf:close();
Retrieving data ...