How to include a text file into the project?

Hi all,

I am trying to include a text file (about 5MB) into the project. It will be read only. How can I access the file?

I tried:

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local file, errorString = io.open( path, "r" )

but it didn’t find the file.

Also, if I want to do a search in the file (assuming the file is in alphabetical order) would I read it sequentially or am I better using a seek function to speed things up? (i.e. does a seek function speed things up or does it search sequentially as well)

Finally, am I better putting it in a lua file as a table? Would that use too much runtime device memory?

Many thanks.

System.ResourceDirectory

As for searching, an sqlite database would be much better.

What’s the content of the file and the searches you’re going to do within them?

It’s a word list (a dictionary) to check if the word entered by the user is a correct word.

Then, by far the most simple and efficient datastructure is to read the dict into a table, use the words as key and a boolean true as a value.

[lua]

local path = system.pathForFile( “words.txt”, system.ResourceDirectory )

local dict = {}

for line in io.lines( path ) do

    dict[line] = true

end

if dict[“whateverword”] then

    – word exists

end

[/lua]

@nick_sherman & @Michael Flad

Thanks to you both.  I found all the answers useful.

System.ResourceDirectory

As for searching, an sqlite database would be much better.

What’s the content of the file and the searches you’re going to do within them?

It’s a word list (a dictionary) to check if the word entered by the user is a correct word.

Then, by far the most simple and efficient datastructure is to read the dict into a table, use the words as key and a boolean true as a value.

[lua]

local path = system.pathForFile( “words.txt”, system.ResourceDirectory )

local dict = {}

for line in io.lines( path ) do

    dict[line] = true

end

if dict[“whateverword”] then

    – word exists

end

[/lua]

@nick_sherman & @Michael Flad

Thanks to you both.  I found all the answers useful.