201 posts
Location
United Kingdom
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?
411 posts
Posted 07 March 2012 - 10:32 PM
hHandle.write(information .. "n")
201 posts
Location
United Kingdom
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.
454 posts
Location
London
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".
201 posts
Location
United Kingdom
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!