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

Attempt to index ? on opening file and editing

Started by CypherAJ, 15 June 2013 - 05:52 AM
CypherAJ #1
Posted 15 June 2013 - 07:52 AM
I'm currently creating a control network for my RP2 Quarry. This is a program for information exchange between control center and drill.

function checkState ()
  local file1 = fs.open("state","r")
  return(file1.readLine())
end
function setState (state)
  local file = fs.open("state","w")
  file.write(state)
  file.close()
end
rednet.open("right")
while true do
  id, msg = rednet.receive()
  if ((id == 8) and (msg ~= "state")) then
    setState(msg)
  end
  if (msg == "state") then
    rednet.send(id,checkState())
  end
end
On running it I get Attempt to index ? (a nil value) on string 3. This means that file wrapping failed. But state file IS there.
If I try to edit state or statecontrol right after error, I get same error on edit:51. If I edit file that doesn't exist, I can't save it (Error saving to 123, for example).
Is this a bug in CC 1.5(FTB Ultimate 1.1.2) or in my program?
theoriginalbit #2
Posted 15 June 2013 - 07:56 AM
could be to do with the fact that you haven't closed the file handle in checkState()


function checkState()
  local file = fs.open("state","r")
  local line = file.readLine()
  file.close()
  return line
end
CypherAJ #3
Posted 15 June 2013 - 09:11 AM
thanks, looks like the issue is fixed. I tried closing file but now I see that I did it after return(), so it didn't work.
theoriginalbit #4
Posted 15 June 2013 - 09:21 AM
yeh. no problems. untested, but this may also work.


function checkState()
  local file = fs.open("state", 'r')
  return file.readLine(), file.close()
end
CypherAJ #5
Posted 15 June 2013 - 03:23 PM
As far as I know Lua, it will 'cause it will need to close file to return the result of fs.close().
theoriginalbit #6
Posted 15 June 2013 - 10:06 PM
well fs.close returns nil, so doing it like I did wouldn't `dirty` the return values.