This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darkroom's profile picture

[Lua] File handling is acting strange

Started by darkroom, 01 March 2013 - 11:38 AM
darkroom #1
Posted 01 March 2013 - 12:38 PM
I need a file that holds a string that represents a boolean but I think I am doing something wrong.
Here is my code anyone got a better way to do it?

local firstTime = io.open("/firstTime")
local isFirstTime = firstTime:read()
local nameStr = ""
if isFirstTime == "true" then
  firstTime:write("false")
  nameStr = "New Game"
else
  nameStr = "Start Game"
end
firstTime:close()
Kingdaro #2
Posted 01 March 2013 - 01:08 PM
If it's the first time, would the file exist in the first place?

Perhaps, if you're storing some sort of configuration, you could just check if the configuration file exists, and if not, it's the first time.
darkroom #3
Posted 01 March 2013 - 01:17 PM
That is a much better idea….. Thanks alot!
oeed #4
Posted 01 March 2013 - 01:19 PM
If it's the first time, would the file exist in the first place?

Perhaps, if you're storing some sort of configuration, you could just check if the configuration file exists, and if not, it's the first time.

If the file doesn't exist it crashes, attempt to index nil.

Darkroom, your problem is in the first line.
You aren't specifying a mode to use, so by default it's using 'r' (take a look t http://lua-users.org/wiki/IoLibraryTutorial).
You can't write using more 'r'.

local firstTime = io.open("/firstTime")
FUCKCOMPUTERCRAFT!"£ #5
Posted 02 March 2013 - 02:43 PM
You have true as "true", should just be true.
ChunLing #6
Posted 03 March 2013 - 12:05 AM
No, firstTime:read() returns a string (if it works at all).

But as mentioned, just using "if firstTime then" works fine for checking if the configuration file has been written yet, if you try to open it in read mode.