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

[LUA] [Error] Editing files

Started by gamesaucer, 14 July 2012 - 02:33 PM
gamesaucer #1
Posted 14 July 2012 - 04:33 PM
I want to save a path for each computer ID that communicates with this server, and I save it in the format "[COMPUTER_ID].path".

Stub file contains the default starting directory, which is in this case "/" (on a single line).
I did this so I can change the starting directory easily later on.

Reading works fine, but once I wrote to the file once, readFolder(i) gives errors on me.
If I try to edit the file myself, I get the error: "edit:284: bad argument: string expected, got nil"

Explanation:
writeFolder([COMPUTER_ID],[PATH TO STORE])
in this case I enter "/lol" as the second argument.


function writeFolder(i,path)
 local path = "disk/paths/" .. i .. ".path"
 local file = fs.open(path,"w")
 file.write(path)
end

function readFolder(i)
 local path = "disk/paths/" .. i .. ".path"
 print(path)
 if (fs.exists(path)) then
  local file = fs.open(path,"r")
  local returnvalue = file.readLine()
  print(returnvalue)
  file.close()
  return returnvalue
 else
  fs.copy("disk/stub.path","disk/paths/" .. i .. ".path")
  return "/"
 end
end
Pinkishu #2
Posted 15 July 2012 - 12:09 PM
I know that edit error :P/>/> happens when you try to edit an empty file (as in completely empty)
Not sure on the readFolder error but if theres nothing i guess readLine might bug out too or atleast return nil
gamesaucer #3
Posted 15 July 2012 - 08:14 PM
in that case the writeFolder function writes a completely empty file. But why is that happening?
MysticT #4
Posted 15 July 2012 - 08:33 PM
Always close the file handles, this happens if you don't.
The problem is that you try to open a file that is already open.
Add file.close() at the end of the writeFolder function and it should work.
gamesaucer #5
Posted 16 July 2012 - 08:44 AM
I'm so stupid, why didn't I see that?
Ah well, I guess it's just like forgetting the semicolon in some other languages.