Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to include a text file into the project?
Started by ammar71 May 07 2019 03:39 AM

6 replies to this topic
files

Best Answer nick_sherman , 07 May 2019 - 03:50 AM

System.ResourceDirectory

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 32 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,790 posts
  • Corona SDK

  Best Answer

System.ResourceDirectory

[TOPIC: post.html]
#3

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,790 posts
  • Corona SDK

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

[TOPIC: post.html]
#4

Michael Flad

[GLOBAL: userInfoPane.html]
Michael Flad
  • Contributor

  • 230 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 32 posts
  • Corona SDK

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.

[TOPIC: post.html]
#6

Michael Flad

[GLOBAL: userInfoPane.html]
Michael Flad
  • Contributor

  • 230 posts
  • Corona SDK

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.

 

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


[TOPIC: post.html]
#7

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 32 posts
  • Corona SDK

@nick_sherman & @Michael Flad

 

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




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: files