Trying to write data to a file

I want to write a button’s id to a txt file, but every time I run this function:

local function checkfile() if not file then file:write( saveData ) io.close( file ) else local contents = file:read("\*a") io.close(file) print(contents) end end

It always prints nil. This is my code so far:

local function writeFile(event) local button = event.target local saveData = button.id local path = system.pathForFile("file.txt", system.DocumentsDirectory) local file, errorString = io.open(path, "w") local function checkfile() if not file then file:write( saveData ) io.close( file ) else local contents = file:read("\*a") io.close(file) print(contents) end end if event.phase == "began" then if button.id == "pacifist" then text = "Good, mercy is the way to go." myText.text = text myText:setFillColor(0, 1, 1) checkfile() end if button.id == "genocide" then text = "You monster" myText.text = text myText:setFillColor(1, 0, 1) checkfile() end end end

I am very confused.

Hi.  As an SSK2owner, you’re working too hard.  If you’re doing this to learn about io.* then my apologies, but if you simply want to write to, append to, and/or read from files you can use the io.* extensions from SSK2.
 
https://roaminggamer.github.io/RGDocs/pages/SSK2/extensions/#io
 
Write To A File (destroys existing file)

local data = "SSK2 Rocks! \n" io.writeFile( data, "someFile.txt" ) -- Will be written to system.DocumentsDirectory 

Append To A File

local data = "It makes your life easier and coding faster." io.appendFile( data, "someFile.txt" ) -- Will be written to system.DocumentsDirectory

Read A File

local data = io.readFile( "someFile.txt" ) print( data ) -- Prints this to console: -- SSK2 Rocks! -- It makes your life easier and coding faster.

PS - Also, if you want to learn about io.* you can compare your code to mine since you do have SSK2.

Wow, I had know idea SSK2 covered so much stuff!

Thanks, I will give these functions a try.

Appending is just what I needed.

I am having a problem, I deleted the file in my Project Sandbox where all the information is saved and my buttons don’t create a new file to replace it: 

local function writeFile(event) local button = event.target local saveData = button.id.."\n" local function checkPath() if io.exists("path.txt") == true then io.appendFile(saveData, "path.txt") elseif io.exists("path.txt") == false then io.writeFile(saveData, "path.txt") end end if event.phase == "began" then if button.id == "pacifist" then text = "Good, mercy is the way to go." myText.text = text myText:setFillColor(0, 1, 1) checkPath() end if button.id == "genocide" then text = "You monster" myText.text = text myText:setFillColor(1, 0, 1) checkPath() end end end

The checkPath function should create a new file if it does not exist, but it isn’t creating a new file.

I added some print statements and the elseif part of the checkPath function was not being called. I also made sure there is no path.txt file in my computer.

Please lead with this next time… :slight_smile:

Also, I didn’t see any print statements so I assumed you hadn’t verified this.

Checking.

Your code is wrong.  

  1. You’re assuming the return type is a boolean.  I believe it is nil for false.

  2. You don’t need an elseif case, that is a waste of execution and logically redundant.

There are only two possibilities, either it exists or does not.  The use of an else if implies there is some third possibility.

Try this:

 if( io.exists("path.txt") ) then io.appendFile(saveData, "path.txt") else io.writeFile(saveData, "path.txt") end

Hi.  As an SSK2owner, you’re working too hard.  If you’re doing this to learn about io.* then my apologies, but if you simply want to write to, append to, and/or read from files you can use the io.* extensions from SSK2.
 
https://roaminggamer.github.io/RGDocs/pages/SSK2/extensions/#io
 
Write To A File (destroys existing file)

local data = "SSK2 Rocks! \n" io.writeFile( data, "someFile.txt" ) -- Will be written to system.DocumentsDirectory 

Append To A File

local data = "It makes your life easier and coding faster." io.appendFile( data, "someFile.txt" ) -- Will be written to system.DocumentsDirectory

Read A File

local data = io.readFile( "someFile.txt" ) print( data ) -- Prints this to console: -- SSK2 Rocks! -- It makes your life easier and coding faster.

PS - Also, if you want to learn about io.* you can compare your code to mine since you do have SSK2.

Wow, I had know idea SSK2 covered so much stuff!

Thanks, I will give these functions a try.

Appending is just what I needed.

I am having a problem, I deleted the file in my Project Sandbox where all the information is saved and my buttons don’t create a new file to replace it: 

local function writeFile(event) local button = event.target local saveData = button.id.."\n" local function checkPath() if io.exists("path.txt") == true then io.appendFile(saveData, "path.txt") elseif io.exists("path.txt") == false then io.writeFile(saveData, "path.txt") end end if event.phase == "began" then if button.id == "pacifist" then text = "Good, mercy is the way to go." myText.text = text myText:setFillColor(0, 1, 1) checkPath() end if button.id == "genocide" then text = "You monster" myText.text = text myText:setFillColor(1, 0, 1) checkPath() end end end

The checkPath function should create a new file if it does not exist, but it isn’t creating a new file.

I added some print statements and the elseif part of the checkPath function was not being called. I also made sure there is no path.txt file in my computer.

Please lead with this next time… :slight_smile:

Also, I didn’t see any print statements so I assumed you hadn’t verified this.

Checking.

Your code is wrong.  

  1. You’re assuming the return type is a boolean.  I believe it is nil for false.

  2. You don’t need an elseif case, that is a waste of execution and logically redundant.

There are only two possibilities, either it exists or does not.  The use of an else if implies there is some third possibility.

Try this:

 if( io.exists("path.txt") ) then io.appendFile(saveData, "path.txt") else io.writeFile(saveData, "path.txt") end