Reading and writing files in the simulator?!

Hi,  
 
I’m using these read / write functions to read / write a high score…
 
 

function writeToFile( data, filename, baseDirectory )     local baseDirectory = baseDirectory or system.DocumentsDirectory     local path = system.pathForFile( filename, baseDirectory )     local file = io.open( path, "w" )     file:write( data )     io.close( file ) end function readFromFile( filename, baseDirectory )     local baseDirectory = baseDirectory or system.ResourceDirectory     local path = system.pathForFile( filename, baseDirectory )     local file = io.open( path, "a+" )     local data = file:read( "\*n" )     io.close( file )     return data end  

 

I call them like this…
 

function getHighScore()    local score = tools.readFromFile("KeePeeUppy.txt", system.DocumentsDirectory)         if (score == nil) then       score = 0    end    return score end function saveHighScore(score)    tools.writeToFile( score, "KeePeeUppy.txt", system.DocumentsDirectory ) end

a print statement shows the score being written, but when I read back it’s always nil.

Any ideas?!?!   TA :slight_smile:

Changed it to this to make sure the file is created if it doesn’t exist but still it doesn’t read the data back in!

function readFromFile( filename, baseDirectory )     local baseDirectory = baseDirectory or system.ResourceDirectory     local path = system.pathForFile( filename, baseDirectory )     local file = io.open( path, "r" )          print(filename)     print(baseDirectory)     print(path)     if file then         print("found file")         local data = file:read( "\*n" )     else         print("creating file")         file = io.open( path, "w" )         file:write(0)     end          io.close( file )     return data end  

Ah fixed it…

function readFromFile( filename, baseDirectory )     local baseDirectory = baseDirectory or system.ResourceDirectory     local path = system.pathForFile( filename, baseDirectory )     local file = io.open( path, "r" )     local data         print(filename)     print(baseDirectory)     print(path)     if file then         print("found file")         data = file:read( "\*n" )     else         print("creating file")         file = io.open( path, "w" )         file:write(0)     end          io.close( file )     return data end  

Glad you fixed it.  But for everyone’s benefit, you were opening the file in Read/Write Append mode (a+).  According to the docs:

a+": append update mode (read/write); previous data is
preserved, writing is only allowed at the end of file. The file pointer
is at the end of the file if the file exists. The file opens in the
append mode. If the file does not exist, it creates a new file for
reading and writing.

So even though the file was readable, the read/write point (file pointer) was at the end of the file, so there was nothing to read, and it returned nil.  Changing to “r” mode (read mode) should have solved your problem.

Yup, a case of DOH!

Thanks for replying :slight_smile:

Changed it to this to make sure the file is created if it doesn’t exist but still it doesn’t read the data back in!

function readFromFile( filename, baseDirectory )     local baseDirectory = baseDirectory or system.ResourceDirectory     local path = system.pathForFile( filename, baseDirectory )     local file = io.open( path, "r" )          print(filename)     print(baseDirectory)     print(path)     if file then         print("found file")         local data = file:read( "\*n" )     else         print("creating file")         file = io.open( path, "w" )         file:write(0)     end          io.close( file )     return data end  

Ah fixed it…

function readFromFile( filename, baseDirectory )     local baseDirectory = baseDirectory or system.ResourceDirectory     local path = system.pathForFile( filename, baseDirectory )     local file = io.open( path, "r" )     local data         print(filename)     print(baseDirectory)     print(path)     if file then         print("found file")         data = file:read( "\*n" )     else         print("creating file")         file = io.open( path, "w" )         file:write(0)     end          io.close( file )     return data end  

Glad you fixed it.  But for everyone’s benefit, you were opening the file in Read/Write Append mode (a+).  According to the docs:

a+": append update mode (read/write); previous data is
preserved, writing is only allowed at the end of file. The file pointer
is at the end of the file if the file exists. The file opens in the
append mode. If the file does not exist, it creates a new file for
reading and writing.

So even though the file was readable, the read/write point (file pointer) was at the end of the file, so there was nothing to read, and it returned nil.  Changing to “r” mode (read mode) should have solved your problem.

Yup, a case of DOH!

Thanks for replying :slight_smile: