Build error with read/write file

I have this code, which should create file with argument if app is running for the first time.

file = io.open("config.txt", "r") if file == nil then   local t = {     ["scoreNum"] = 0,     ["tableOfNum"] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},     ["highScore"] = 0}   file:write(tostring(json.encode( t )))   file:close() else   file:close() end

In Corona simulator I had no problem with this code, everything works well, but after building project I had this: “attempt to index local ‘file’ (a nil value)” in line file:write(tostring(json.encode( t )))

Also I tried to change the build.settings file (line below) but it did not help

android = { usesPermissions = {       "android.permission.MANAGE\_DOCUMENTS", }, },

What am I doing wrong?

(sorry for my English if something do not understand - write

можно написать на русском, но на всякий случай продублируйте на Английском, вдруг админам не понравится)

It’s likely you are trying to write to system.ResourceDirectory which on device is read only. You need to use system.pathForFile() to create a path to the system.DocumentsDirectory, something like:

local path = system.pathForFile( "config.txt", system.DocumentsDirectory) local file = io.open(path, "w")

You also need to open the file for writing if you intend to write to it. The “r” says to open the file for reading.

Rob

It’s likely you are trying to write to system.ResourceDirectory which on device is read only. You need to use system.pathForFile() to create a path to the system.DocumentsDirectory, something like:

local path = system.pathForFile( "config.txt", system.DocumentsDirectory) local file = io.open(path, "w")

You also need to open the file for writing if you intend to write to it. The “r” says to open the file for reading.

Rob