Creating a file if it doesn't exists?

Hello, I am using the scoring module here provided by corona:

https://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/

so I tweaked it a bit to check the prev score and compare it with the current score if  current score is greater than it updates and saves the new score or else does not save it.

It works fine if I manually create the file in the sandbox where it should be saving scores. But when the application will be run first time their won’t be any error hence it gives error that file does not exisits.

How can I create a file in the sandbox only once? It checks to see if their is a file if their is it does not create any or if it isn’t their it creates a new file?

Please help, It’s been days since I’ve been trying to get around this.

Hi!

Search around the forum a bit. It’s full of solutions that do a variation on the following:

  1. Open a certain file.

  2. If the file returns nil it doesn’t exist, so create and save a file instead.

This way you will create the file when you use the app for the first time. From then on the file exist and you will be able to open it.

@thomas

I have tried a certain things, I am able to create a file now if it dosen’t exists. But then I am not able to write anything to it later in the code. Why?

Hi, I don’t have the time to help you out more, but here’s some code I used for this.

Good luck!

-- SETUP HIGHSCORES !!! local json = require("json") local path = system.pathForFile( "highscores.txt", system.DocumentsDirectory ) -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) if file then print("Highscore File Found!") -- read all contents of file into a string local contents = file:read( "\*a" ) highscoreTable = json.decode(contents) TrialsLeft = highscoreTable[23] if highscoreTable[12] == 1 then fireBombUnlocked = true else fireBombUnlocked = false end print(highscoreTable[1]) io.close( file ) else -- create file because it doesn't exist yet fireBombUnlocked = false print("Creating New Highscore File!") file = io.open( path, "w" ) -- the 11th value is the volume, the 12th is a flag to set the unlock status!!! highscoreTable = {10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000, 100, 0, 10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000, 4} TrialsLeft = highscoreTable[23] local highscoreTableJSON = json.encode(highscoreTable) file:write(highscoreTableJSON) io.close( file ) end

Thanks for the help, I did it but some other way

Hi!

Search around the forum a bit. It’s full of solutions that do a variation on the following:

  1. Open a certain file.

  2. If the file returns nil it doesn’t exist, so create and save a file instead.

This way you will create the file when you use the app for the first time. From then on the file exist and you will be able to open it.

@thomas

I have tried a certain things, I am able to create a file now if it dosen’t exists. But then I am not able to write anything to it later in the code. Why?

Hi, I don’t have the time to help you out more, but here’s some code I used for this.

Good luck!

-- SETUP HIGHSCORES !!! local json = require("json") local path = system.pathForFile( "highscores.txt", system.DocumentsDirectory ) -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) if file then print("Highscore File Found!") -- read all contents of file into a string local contents = file:read( "\*a" ) highscoreTable = json.decode(contents) TrialsLeft = highscoreTable[23] if highscoreTable[12] == 1 then fireBombUnlocked = true else fireBombUnlocked = false end print(highscoreTable[1]) io.close( file ) else -- create file because it doesn't exist yet fireBombUnlocked = false print("Creating New Highscore File!") file = io.open( path, "w" ) -- the 11th value is the volume, the 12th is a flag to set the unlock status!!! highscoreTable = {10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000, 100, 0, 10000, 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000, 4} TrialsLeft = highscoreTable[23] local highscoreTableJSON = json.encode(highscoreTable) file:write(highscoreTableJSON) io.close( file ) end

Thanks for the help, I did it but some other way