Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Build error with read/write file
Started by kuzmin2002 Jan 11 2019 03:34 AM

1 reply to this topic

Best Answer Rob Miracle , 11 January 2019 - 09:32 AM

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

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

kuzmin2002

[GLOBAL: userInfoPane.html]
kuzmin2002
  • Observer

  • 1 posts
  • Corona SDK

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

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



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,147 posts
  • Corona SDK

  Best Answer

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


  • kuzmin2002 likes this


[topic_controls]
[/topic_controls]