[RESOLVED] system.DocumentsDirectory not writable?

Hi,

I have the following code:

local filePath = system.pathForFile( "data.txt", system.DocumentsDirectory ) local file local reason local highscore local localScore = event.params.data; function loadHighScore() file, reason = io.open(filePath, "r" ) if file then -- read all contents of file into a string highscore = file:read( "\*a" ) else file = io.open( filePath, "w" ) file:write(0) highscore=localScore end io.close( file ) end function saveHighScore() file = io.open( filePath, "w" ) file:write(localScore) end loadHighScore()

So basically I load the highscore and it checks for a file, if the file doesn’t exist it creates a new one and makes the highscore the same as the local score… the next time it does this, the file WILL exist so it takes the highscore from the file and shows them that.

This works perfectly in the simulator but on devices it never seems to actually write the file, meaning the highscore is always reset.

I did have this set as the resourceDirectory which worked however I found that was a bad idea as when I updated the app it would be reset.

Why is this not working? My permissions are:

usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_NETWORK\_STATE", "com.android.vending.CHECK\_LICENSE", "android.permission.WRITE\_EXTERNAL\_STORAGE", "android.permission.READ\_PHONE\_STATE" },

Thanks

Kevin

Opening a file with “w” overwrites the existing contents.   Try opening with “a+”, which will open in append/update mode.

http://docs.coronalabs.com/api/library/io/open.html

This is how I write to my log file in the system.DocumentsDirectory…

function LogFile:write(message) self:checkForTruncate() datafile, errStr = io.open(self.filePath, "a+") if not errStr and message then datafile:write(Tools.formatTimestamp(os.time()) .. " - " .. message .. "\n") datafile:close() end end

I understand that “w” overwrites, but that is ok, the only time it will get to overwrite is if it gets in the ‘saveHighScore’ function, which only gets hit if localscore is higher than highscore or if the file doesn’t exist in which case it writes the local score as the high score…

If the file does exist though it should get the highscore first - it works as expected on the simulator its only on device which makes me think it might be permissions?!

Don’t you need to close the file in saveHighScore()?

I do yes… would this make a difference though - seen as though it does still work in the simulator??

Maybe the io implementation on your computer flushes automatically after writes, but the device doesn’t.   In any case, I’d close it.  :-)

Looks like closing the input file has done the job, I was noticing the file was randomly changing as well, all sorted now!

Thanks for the advice! Game is now on the play store

Kevin

Opening a file with “w” overwrites the existing contents.   Try opening with “a+”, which will open in append/update mode.

http://docs.coronalabs.com/api/library/io/open.html

This is how I write to my log file in the system.DocumentsDirectory…

function LogFile:write(message) self:checkForTruncate() datafile, errStr = io.open(self.filePath, "a+") if not errStr and message then datafile:write(Tools.formatTimestamp(os.time()) .. " - " .. message .. "\n") datafile:close() end end

I understand that “w” overwrites, but that is ok, the only time it will get to overwrite is if it gets in the ‘saveHighScore’ function, which only gets hit if localscore is higher than highscore or if the file doesn’t exist in which case it writes the local score as the high score…

If the file does exist though it should get the highscore first - it works as expected on the simulator its only on device which makes me think it might be permissions?!

Don’t you need to close the file in saveHighScore()?

I do yes… would this make a difference though - seen as though it does still work in the simulator??

Maybe the io implementation on your computer flushes automatically after writes, but the device doesn’t.   In any case, I’d close it.  :-)

Looks like closing the input file has done the job, I was noticing the file was randomly changing as well, all sorted now!

Thanks for the advice! Game is now on the play store

Kevin