[Resolved] Problem reading text files

I’m trying to read a text file that’s in a folder named data. The data folder is in my project folder. I’m following the tutorial directions.

The code is:
function getMyDbFile(fName)
local doc_path,xText,myFile
myFile = fName
local doc_path = system.pathForFile(myFile,system.DocumentsDirectory)
print(“filename: " … tostring(doc_path))
xfile = io.open(doc_path, “r”)
if xfile then --nil if no file found
local contents = xfile:read(”*a")
io.close(xfile)
print(content)
end
end
The filename is “ContinentsDB” and it’s in folder “data”
doc_path = /Users/williamprothero/Library/Application Support/Corona Simulator/MapGame-E8B0214761B80931B11BED6D8038A65C/Documents/data/ContinentsDB.txt

The xfile variable evaluates to nil.
So, it appears that the file is not being found. Yet, it does exist. However, it doesn’t exist in the location that is found by doc_path, which is a sandbox location, I guess. Do I need to manually add the data files to the sandbox??

What’s going on?

[import]uid: 151957 topic_id: 29250 reply_id: 329250[/import]

Forget replying. I found that I, indeed, needed to add the data files to the sandbox. I thought they would be read from the data folder that was in my project folder, which seemed more intuitive, but…

It would be nice if the docs on reading and writing files would say something about the sandbox. It can get frustrating for newbies.
[import]uid: 151957 topic_id: 29250 reply_id: 117616[/import]

Just to further clarify, the folder in which your project is stored (i.e., all of your code, graphics, sounds, and other assets) is the system.ResourceDirectory. This directory is read-only.

The system.DocumentsDirectory is a directory where you can save files that should persist between runs of your application. When your application is first installed, it will be empty.

In your case, I’m guessing that the text file you’re trying to read is static data needed for the game about the continents. In that case, you would simply save it in your project folder (as you initially said you did) and access it via system.ResourceDirectory.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 29250 reply_id: 117630[/import]

Thanks! That makes perfect sense. [import]uid: 151957 topic_id: 29250 reply_id: 117641[/import]

Thank you - @prothero for updating and @Andrew for the additional clarification.

Marking as resolved!

Peach :slight_smile: [import]uid: 52491 topic_id: 29250 reply_id: 117703[/import]

Hey, @aukStudios:
If you update an app (which i’m guessing is just reinstalling it with the new stuff) made with corona, is the sandbox kept or emptied? I’m assuming it’s kept. If it’s emptied, how would you keep save data past updates?

[import]uid: 147322 topic_id: 29250 reply_id: 117733[/import]

I haven’t released or updated an app myself yet, but I’m fairly certain that the system.DocumentsDirectory (which is part of the Sandbox) is retained during an update, while the system.ResourceDirectory is overwritten with the updated app. The only time the system.DocumentsDirectory is lost is when a user actually deletes the app from their device.

  • Andrew [import]uid: 109711 topic_id: 29250 reply_id: 117803[/import]