problem wtih save data in a textfile

hi,

i have this problem when i save my number of level in a textfile my function start don’t work and an another thing is that the terminal output of my corona simulator restarts when i save my data in my textfile…

local path = system.pathForFile( "myfile.txt", system.RessourceDirectory ) local file = io.open( path, "r" ) var = file:read( "\*a" ) io.close( file ) file = nil local cntLevel=tonumber(var) local function upgrade() cntLevel=cntLevel+1 if cntLevel \>= #level then print("you have finished the game…hourra!") cntLevel = #level else --save data in text file saveData = cntLevel file = io.open( path, "w" ) file:write( saveData ) io.close( file ) file = nil flagupgradelevel=true local function startLevel() print("start") end timer.performWithDelay(2000,startLevel) end end --result nothing and my terminal output restart but the file is correctly saved

by against when i don’t save my data my function works

local function upgrade() cntLevel=cntLevel+1 if cntLevel \>= #level then print("you have finished the game…hourra!") cntLevel = #level else local function startLevel() print("start") end timer.performWithDelay(2000,startLevel) end end --result : start

what’s the problem with this function yet I followed the documentation…

thanks for your help

solved. By better reading the documentation

This is important because, for security reasons, you can not write/save data to system.ResourceDirectory.

so with

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

no problem! The file myfile.txt must be created and located via Corona simulator>show project Sandbox :slight_smile:

I still have a problem when i publish my application on android it’s seems that myfile.txt or my DocumentsDirectory is not recognize in the phone…

/Users/jenkins/slaveroot/workspace/Templates/label/android/subrepos/composer/composer.lua:1460:X\...\game/lua:35:attempt to index local 'file' (a nil value)

why? 

Why are you niling file?

local path = system.pathForFile( "myfile.txt", system.RessourceDirectory ) local file = io.open( path, "r" ) var = file:read( "\*a" ) io.close( file ) file = nil

hi undecode,

i have simply followed the documentation

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

like you i don’t understand why they put file = nil

i have tried with uncomment these lines in my application

> in corona simulator : no problem my app works (it’was also the case before without uncomment these lines)

> in my android phone : i have no more error but i have a blackscreen and nothing appears …

exactly the same problem with an another android phone.

i don’t understand why…if you could help me…

You nil file because it contains more than a simple variable, so it takes up a little bit of memory. Nilling it out frees that little bit of memory up.

As for your problem, you can’t call file:read() if the file doesn’t exist. When you run your app the first time, if your file doesn’t exist, then you create a file for future use:

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local file = io.open( path, "r" ) if file then -- the file exists     var = file:read( "\*a" )     io.close( file )     file = nil else -- file doesn't exist:      file = io.open( path, "w" )      local startingLevel = 1      file:write( startingLevel )      io.close( file )      var = startingLevel end

Rob

or something like that…

hi Rob,

i believed that the files in the sandbox directory were copied with the app during the build… Now it works. TKY

solved. By better reading the documentation

This is important because, for security reasons, you can not write/save data to system.ResourceDirectory.

so with

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

no problem! The file myfile.txt must be created and located via Corona simulator>show project Sandbox :slight_smile:

I still have a problem when i publish my application on android it’s seems that myfile.txt or my DocumentsDirectory is not recognize in the phone…

/Users/jenkins/slaveroot/workspace/Templates/label/android/subrepos/composer/composer.lua:1460:X\...\game/lua:35:attempt to index local 'file' (a nil value)

why? 

Why are you niling file?

local path = system.pathForFile( "myfile.txt", system.RessourceDirectory ) local file = io.open( path, "r" ) var = file:read( "\*a" ) io.close( file ) file = nil

hi undecode,

i have simply followed the documentation

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

like you i don’t understand why they put file = nil

i have tried with uncomment these lines in my application

> in corona simulator : no problem my app works (it’was also the case before without uncomment these lines)

> in my android phone : i have no more error but i have a blackscreen and nothing appears …

exactly the same problem with an another android phone.

i don’t understand why…if you could help me…

You nil file because it contains more than a simple variable, so it takes up a little bit of memory. Nilling it out frees that little bit of memory up.

As for your problem, you can’t call file:read() if the file doesn’t exist. When you run your app the first time, if your file doesn’t exist, then you create a file for future use:

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) local file = io.open( path, "r" ) if file then -- the file exists     var = file:read( "\*a" )     io.close( file )     file = nil else -- file doesn't exist:      file = io.open( path, "w" )      local startingLevel = 1      file:write( startingLevel )      io.close( file )      var = startingLevel end

Rob

or something like that…

hi Rob,

i believed that the files in the sandbox directory were copied with the app during the build… Now it works. TKY