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

FileSystem, fs.write/writeLine() - Error

Started by Xemiru, 24 November 2012 - 01:56 PM
Xemiru #1
Posted 24 November 2012 - 02:56 PM
I've been working on fixing this glitch long enough, I can't figure this out. Every time, it makes the non-sensible error (to me) that I'm attempting to index a nil value.

   datafile = fs.open("xirc/settings.xicfg", "w")
   print("Enter your name for this computer.")
   cx,cy = term.getCursorPos()
   write(">> ")
   local nameinput = io.read()
   datafile:write("name="..nameinput.."\n") --error
It's one of the two LAST GLITCHES in my code.
Luanub #2
Posted 24 November 2012 - 02:59 PM
It's failing because you're trying to read the file using IO when you've opened it using fs. You will need to do fs.read() or fs.readLine().

Oh and you're using : with your datafile:write(). With fs it is a . and not a : so it should be.


   datafile = fs.open("xirc/settings.xicfg", "w")
   print("Enter your name for this computer.")
   cx,cy = term.getCursorPos()
   write(">> ")
   local nameinput = fs.read()
   datafile.write("name="..nameinput.."n")
Edited on 24 November 2012 - 02:01 PM
OmegaVest #3
Posted 24 November 2012 - 03:01 PM
Er, stupid question, but I've found stupid questions to be my solution a bunch of the time, so. . .

Have you tried using datafile.write() instead of datafile:write()? It shouldn't matter, I think, but then, sometimes I get small differences wrong.


EDIT: Drat, ninja'd again.
Luanub #4
Posted 24 November 2012 - 03:04 PM
Have you tried using datafile.write() instead of datafile:write()? It shouldn't matter, I think, but then, sometimes I get small differences wrong.

In most cases with Lua it shouldn't matter if you use a . or a : but with CC and the fs/io API's it does actually matter. I think it is due to the fact that the IO API in CC is not the regular Lua IO API but rather a wrapper around FS.
Xemiru #5
Posted 24 November 2012 - 03:09 PM
The io.read() is to read the input from the user, not to read from the file.
Yes, I've tested both file:function and file.function.
Xemiru #6
Posted 24 November 2012 - 03:12 PM
And it still doesn't work.
Luanub #7
Posted 24 November 2012 - 03:25 PM
Oh in that case it might be getting confused by the io.read() since you have a file handle open. Try using just read() and not io.read() and make sure to use .'s with the fs API. You could also try putting the read() prior to your file handling.


print("Enter your name for this computer.")
cx,cy = term.getCursorPos()
write(">> ")
local nameinput = read()
datafile = fs.open("xirc/settings.xicfg", "w")
datafile.write("name="..nameinput.."n")
Lyqyd #8
Posted 24 November 2012 - 03:55 PM
In most cases with Lua it shouldn't matter if you use a . or a :

This is simply not true. Use of a colon or period determines what the first argument of the called function will be.
ChunLing #9
Posted 24 November 2012 - 04:15 PM

   print("Enter your name for this computer.")
   cx,cy = term.getCursorPos()
   write(">> ")
   local nameinput = io.read()
   datafile = fs.open("xirc/settings.xicfg", "w")
print(type(datafile)) --confirm that you have a table
print(type(datafile.writeLine)) --confirm that you have a function
   datafile.writeLine("name="..nameinput) --change ":" to "." and use writeLine