Jump to content

Reading file line by line


Recommended Posts

Ok, I used example from wiki and explode (founded in Google), works good. Thanks. If somebody would need it, here:

f = fileOpen("thefile.txt", false)
local FullText = fileRead(f, 100)
while not fileIsEOF(f)
do
FullText = FullText .. fileRead(f, 100)
end
fileClose(f)
 
Lines = explode("\n", FullText)
for i = 1, #Lines
do
-- line: Lines[i]
end

Explode function:

function explode (seperator, str)  --from: [url=http://luanet.net/lua/function/explode]http://luanet.net/lua/function/explode[/url]
local pos, arr = 0, {}
for st, sp in function() return string.find( str, seperator, pos, true ) end do -- for each divider found
table.insert( arr, string.sub( str, pos, st-1 ) ) -- Attach chars left of current divider
	pos = sp + 1 -- Jump past current divider
end
table.insert( arr, string.sub( str, pos ) ) -- Attach chars right of last divider
return arr
end

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...