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

[Lua][Question] Writing to new line of file

Started by toxicwolf, 07 March 2012 - 07:52 PM
toxicwolf #1
Posted 07 March 2012 - 08:52 PM
Hi, I currently have this section of code to read a specific line from a file:


local tFile = {}
local hHandle = fs.open("/data/data.dat", "r")
repeat
  local line = hHandle:readLine()
  table.insert(tFile, line)
until not line
hHandle:close()
return tFile[1] -- Line 1 = Local Mode

However, I need to know how to write to certain lines of the file. I can add all the strings to the tFile table, and then write that to the file, but this presents another problem: How do I write one table entry to a line in the file, and then move to the next line to write the next table entry in?
Casper7526 #2
Posted 07 March 2012 - 10:32 PM
hHandle.write(information .. "n")
toxicwolf #3
Posted 08 March 2012 - 07:46 AM
hHandle.write(information .. "n")
Thanks! I've tried putting that into a function I want, and it wont work.


local tFile = {}
local hHandle = fs.open("/data/data.dat", "r")
repeat
  local line = hHandle:readLine()
  table.insert(tFile, line)
until not line
tFile[1] = printMode
hHandle.writeLine(tFile[1]) -- Where Attempt to call nil error occurs
hHandle.writeLine(tFile[2])
hHandle.writeLine(tFile[3])
hHandle:close()
I'm trying to use the previous bit of code to read the file, and put each line into the tFile table. Then it changes a certain table entry (to edit a specific line) and then writes that all back to the file.
However I'm getting an Attempt to call nil error on the first hHandle.writeLine() line.

And yes, the printMode variable is defined, it is a value passed into the function, I just didn't paste that part.
Advert #4
Posted 08 March 2012 - 08:09 AM
hHandle.write(information .. "n")
Thanks! I've tried putting that into a function I want, and it wont work.


local tFile = {}
local hHandle = fs.open("/data/data.dat", "r")
repeat
  local line = hHandle:readLine()
  table.insert(tFile, line)
until not line
tFile[1] = printMode
hHandle.writeLine(tFile[1]) -- Where Attempt to call nil error occurs
hHandle.writeLine(tFile[2])
hHandle.writeLine(tFile[3])
hHandle:close()
I'm trying to use the previous bit of code to read the file, and put each line into the tFile table. Then it changes a certain table entry (to edit a specific line) and then writes that all back to the file.
However I'm getting an Attempt to call nil error on the first hHandle.writeLine() line.

And yes, the printMode variable is defined, it is a value passed into the function, I just didn't paste that part.

You'll need to open a new handle in order to write; the "r" when you open it stands for "read".
toxicwolf #5
Posted 08 March 2012 - 04:02 PM
hHandle.write(information .. "n")
Thanks! I've tried putting that into a function I want, and it wont work.


local tFile = {}
local hHandle = fs.open("/data/data.dat", "r")
repeat
  local line = hHandle:readLine()
  table.insert(tFile, line)
until not line
tFile[1] = printMode
hHandle.writeLine(tFile[1]) -- Where Attempt to call nil error occurs
hHandle.writeLine(tFile[2])
hHandle.writeLine(tFile[3])
hHandle:close()
I'm trying to use the previous bit of code to read the file, and put each line into the tFile table. Then it changes a certain table entry (to edit a specific line) and then writes that all back to the file.
However I'm getting an Attempt to call nil error on the first hHandle.writeLine() line.

And yes, the printMode variable is defined, it is a value passed into the function, I just didn't paste that part.

You'll need to open a new handle in order to write; the "r" when you open it stands for "read".

Ah, of course! Stupid me, this is obvious now that you've pointed it put, thanks!