I’m trying to read a list of items from a file into an array. I find that it works fine with only one item, but if I have more than one item in the file then I get a crash.
My relevant code:
local imageNumber = 1
local contents = file:read( "\*a" )
local myImages = {
contents
}
imageSlide = display.newImage( myImages[imageNumber], baseDir )
Any help would be greatly appreciated. [import]uid: 10903 topic_id: 5089 reply_id: 305089[/import]
Here is the way I do it, but it may not be the best:
[lua]local data = {}
local x = 0
local filePath = system.pathForFile( “file.txt”,system.ResourceDirectory )
for line in io.lines(filePath) do
x = x+1
data[x] = f
end[/lua] [import]uid: 13512 topic_id: 5089 reply_id: 17144[/import]
Actually you would need to put the “x = x+1” increment below the accessing of the array “data[x] = f”, or else you will start on the second line in the array. [import]uid: 7197 topic_id: 5089 reply_id: 17155[/import]
No all I’m saying is that the incrementing comes after the array access. But I like what you did, and I will use your method. I think what I am doing is not enough for DB access. [import]uid: 7197 topic_id: 5089 reply_id: 17158[/import]