Jump to content




FileSystem, fs.write/writeLine() - Error

help

8 replies to this topic

#1 Xemiru

  • Members
  • 38 posts

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.

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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 by luanub, 24 November 2012 - 03:01 PM.


#3 OmegaVest

  • Members
  • 436 posts

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.

#4 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 24 November 2012 - 03:04 PM

 OmegaVest, on 24 November 2012 - 03:01 PM, said:

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.

#5 Xemiru

  • Members
  • 38 posts

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.

#6 Xemiru

  • Members
  • 38 posts

Posted 24 November 2012 - 03:12 PM

And it still doesn't work.

#7 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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")


#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 November 2012 - 03:55 PM

 luanub, on 24 November 2012 - 03:04 PM, said:

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.

#9 ChunLing

  • Members
  • 2,027 posts

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users