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

(A little more) trouble with files.

Started by leftcatcher, 11 October 2012 - 05:01 AM
leftcatcher #1
Posted 11 October 2012 - 07:01 AM
Hello. I'm trying to make a program that controls a redstone output of a certain side. I've been trying to get it all to work, and it's just giving me a lot of trouble. The entire point of having a file control the redstone output is that it will open up the previous state of the output on startup. What I'm trying to have it do is create the directory(easy), then read the file, and if the first line = nil, then it sets a variable to false, and it writes false into the file later on. Then, it reads again and depending on what it is it will display "On" or "Off" (true or false respectively). When you toggle it, it changes the value to the opposite of what it is, and thus displays the opposite thing (which I know how to do). I'm getting one of those "attempt to index ? (a nil value)" things on the marked line:


function saveTopRSState()
  fs.makeDir("RSStates")
  local rstopfile = fs.open("RSStates/top", "r")
  local toprsdata = {}
  local lineA = rstopfile.readLine() -- This line.
  if lineA == nil then
  isTopRSOn = false
  else
  repeat
	table.insert(toprsdata,lineA)
	lineA = rstopfile.readLine()
  until line == nil
  isTopRSOn = toprsdata[1]
end
  local file = fs.open("RSStates/top", "w")
  file.writeLine(isTopRSOn)
  file.close()
end

function writeTopRSStatus(y)
  term.setCursorPos(32,y)
  saveTopRSState()
  local rstopfile = fs.open("RSStates/top", "r")
  local TopRSData = {}
  local line = rstopfile.readLine()
  repeat
	table.insert(TopRSData,line)
	line = rstopfile.readLine()
  until line == nil
  currentTopRS = TopRSData[1]
  if currentTopRS == "true" then
	term.write("ON ")
  else
	term.write("OFF")
end
end

I also notice that there is no "top" file in the RSStates directory when I actually look it up. So, it's messing up, and I can't figure out why. Though I feel the answer is right in front of me and I can't see it. :P/>/>

Something honestly tells me that this is the wrong way to do all this, but I can't figure out how to get it to do this, if you could help me either fix this or make another way, I would be grateful.

Thanks in advance!
Doyle3694 #2
Posted 11 October 2012 - 07:41 AM
IF the file doesn't exist, opening in read mode will just give you nil. open the file in appeal first, to make sure it's there.
Orwell #3
Posted 11 October 2012 - 07:48 AM
* snip *


fs.open("RSStates/top", "r")
This won't create the file if it doesn't exist. It will return a nil value in such case. You might want to do it like this:


local filename = "RSStates/top"
if not fs.exists(filename) then
  fs.makeDir("RSStates")
  local h = fs.open(filename, "w")
  h.close()
end
local h = fs.open(filename, "r")
local line = h.readLine()
h.close()

EDIT: ninja'd..
Doyle3694 #4
Posted 11 October 2012 - 07:49 AM
Get ninja'd, Orwell! :P/>/> ;)/>/> :)/>/>
leftcatcher #5
Posted 11 October 2012 - 08:17 AM
Yeah, sorry for not updating, I figured that out and did change that, but it won't actually write true or false into the file, so it won't display on or off because of that. ._. I'm not sure why it's not writing. Below is all parts of the code (including the ones that are supposed to change the stuff).

Also, keep in mind that I'm pretty much brand new to the whole "messing around with files" thing, so for all I know, the way I have this set up is horrible. ._.



function toggleTopRS() -- This function is called after the next two, so the file does actually exist for it.
if rs.getOutput("top") == true then
  rs.setOutput("top",false)
elseif rs.getOutput("top") == false then
  rs.setOutput("top",true)
end
local file = fs.open("RSStates/top", "r")
local toprsdata = {}
local line = file.readLine()
repeat
  table.insert(toprsdata,line)
  line = file.readLine()
until line == nil
if toprsdata[1] == "false" then
  local rstopfile = fs.open("RSStates/top", "w")
  rstopfile.writeLine("true")
  rstopfile.close()
elseif toprsdata[1] == "true" then
  local rstopfile = fs.open("RSStates/top", "w")
  rstopfile.writeLine("false")
  rstopfile.close()
  end
  inRSOutputsMenu = false
end


function saveTopRSState()
  fs.makeDir("RSStates")
   local file = fs.open("RSStates/top", "w")
  file.close()
  local rstopfile = fs.open("RSStates/top", "r")
  local toprsdata = {}
  local lineA = rstopfile.readLine()
  repeat
	table.insert(toprsdata,lineA)
	lineA = rstopfile.readLine()
  until line == nil
	  if toprsdata[1] == nil then
  toprsdata[1] = false
  else
end
end

function writeTopRSStatus(y)
  term.setCursorPos(32,y)
  saveTopRSState()
  local rstopfile = fs.open("RSStates/top", "r")
  local toprsdata = {}
  local line = rstopfile.readLine()
  repeat
	table.insert(toprsdata,line)
line = rstopfile.readLine()
  until line == nil
  currentTopRS = toprsdata[1]
  if currentTopRS == true then
	term.write("ON ")
  else
	term.write("OFF")
end
end