[Lua]How to read/write certain lines in fi...
brett122798 26 Jul 2012
I would like to know how to read/write a specified line in files so I can make save files and things.
Like maybe I could make functions that look like this:
Like maybe I could make functions that look like this:
writeline("randomfile", 5, "randomtext") readline("randomfile", 5) -- You can use the text from that line in an if statement or something by typing "filetext" -- Ex: if password == filetext then -- <Code>
KingMachine 26 Jul 2012
http://lua-users.org...LibraryTutorial
this is what you want. The forum cuts it off but it's the "IO" tutorial
this is what you want. The forum cuts it off but it's the "IO" tutorial
brett122798 26 Jul 2012
KingMachine, on 26 July 2012 - 06:12 AM, said:
http://lua-users.org...LibraryTutorial
this is what you want. The forum cuts it off but it's the "IO" tutorial
this is what you want. The forum cuts it off but it's the "IO" tutorial
KingMachine 26 Jul 2012
They don't write very well do they? It's the best source of info on the subject though sadly.
Luanub 26 Jul 2012
KingMachines a little off, unfortunately not all of the IO module works in the LuaJ that is being used in CC and there is nothing really in the IO module that will allow you to modify a specific line in a file. However the Lua 5.1 Reference manual is definitely one of the best resources available.
A fairly simple way of doing this is capturing the contents of the file into a table and then with table functions modify the line of your choice. Here is something for you to work with..
Hopefully this helps to get you started. Let me know if you need any further help with it.
A fairly simple way of doing this is capturing the contents of the file into a table and then with table functions modify the line of your choice. Here is something for you to work with..
--First lets read the file and put the contents in a table name tContents local file = io.open("example.txt", "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table --Print a specific line print (tContents[3]) --Modify a specific line table.remove(tContents, 3) -- will remove line 3 so we can insert the new line 3 table.insert(tContents, 3, "New Information") -- inserts the string "New Infomation" on line 3 in the table. --Convert table to string and save the file sContents = textutils.serialize(tContents) local file = io.open("example.txt", "w") file:write(sContents) file:close()
Hopefully this helps to get you started. Let me know if you need any further help with it.
brett122798 26 Jul 2012
luanub, on 26 July 2012 - 08:56 AM, said:
KingMachines a little off, unfortunately not all of the IO module works in the LuaJ that is being used in CC and there is nothing really in the IO module that will allow you to modify a specific line in a file. However the Lua 5.1 Reference manual is definitely one of the best resources available.
A fairly simple way of doing this is capturing the contents of the file into a table and then with table functions modify the line of your choice. Here is something for you to work with..
Hopefully this helps to get you started. Let me know if you need any further help with it.
A fairly simple way of doing this is capturing the contents of the file into a table and then with table functions modify the line of your choice. Here is something for you to work with..
--First lets read the file and put the contents in a table name tContents local file = io.open("example.txt", "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table --Print a specific line print (tContents[3]) --Modify a specific line table.remove(tContents, 3) -- will remove line 3 so we can insert the new line 3 table.inserct(tContents, 3, "New Information") -- inserts the string "New Infomation" on line 3 in the table. --Convert table to string and save the file sContents = textutils.serialize(tContents) local file = io.open("example.txt", "w") file:write(sContents) file:close()
Hopefully this helps to get you started. Let me know if you need any further help with it.
Oh, and thanks a bunch! />
brett122798 27 Jul 2012
I'm getting an error when using this function I made:
Error:
There any fix?
function filereadline(filename, line) local file = io.open(filename, "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table filetext = tContents(line) end
Error:
textutils:141: attempt to concatenate string and nil
There any fix?
Lyqyd 27 Jul 2012
brett122798, on 27 July 2012 - 12:04 AM, said:
I'm getting an error when using this function I made:
Error:
There any fix?
function filereadline(filename, line) local file = io.open(filename, "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table filetext = tContents(line) end
Error:
textutils:141: attempt to concatenate string and nil
There any fix?
Make sure sContents isn't nil.
brett122798 27 Jul 2012
Lyqyd, on 27 July 2012 - 12:17 AM, said:
brett122798, on 27 July 2012 - 12:04 AM, said:
I'm getting an error when using this function I made:
Error:
There any fix?
function filereadline(filename, line) local file = io.open(filename, "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table filetext = tContents(line) end
Error:
textutils:141: attempt to concatenate string and nil
There any fix?
Make sure sContents isn't nil.
Lyqyd 27 Jul 2012
brett122798, on 27 July 2012 - 12:23 AM, said:
Lyqyd, on 27 July 2012 - 12:17 AM, said:
brett122798, on 27 July 2012 - 12:04 AM, said:
I'm getting an error when using this function I made:
Error:
There any fix?
function filereadline(filename, line) local file = io.open(filename, "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table filetext = tContents(line) end
Error:
textutils:141: attempt to concatenate string and nil
There any fix?
Make sure sContents isn't nil.
You can't, not with unserialize. You can, however, throw something like this in there:
if sContents then tContents = textutils.unserialize(sContents) else tContents = {} end
brett122798 27 Jul 2012
Lyqyd, on 27 July 2012 - 12:29 AM, said:
brett122798, on 27 July 2012 - 12:23 AM, said:
Lyqyd, on 27 July 2012 - 12:17 AM, said:
brett122798, on 27 July 2012 - 12:04 AM, said:
I'm getting an error when using this function I made:
Error:
There any fix?
function filereadline(filename, line) local file = io.open(filename, "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table filetext = tContents(line) end
Error:
textutils:141: attempt to concatenate string and nil
There any fix?
Make sure sContents isn't nil.
You can't, not with unserialize. You can, however, throw something like this in there:
if sContents then tContents = textutils.unserialize(sContents) else tContents = {} end
brett122798 27 Jul 2012
Lyqyd, on 27 July 2012 - 12:29 AM, said:
brett122798, on 27 July 2012 - 12:23 AM, said:
Lyqyd, on 27 July 2012 - 12:17 AM, said:
brett122798, on 27 July 2012 - 12:04 AM, said:
I'm getting an error when using this function I made:
Error:
There any fix?
function filereadline(filename, line) local file = io.open(filename, "r") sContents = file:read() -- capture file in a string file:close() tContents = textutils.unserialize(sContents) -- convert string to table filetext = tContents(line) end
Error:
textutils:141: attempt to concatenate string and nil
There any fix?
Make sure sContents isn't nil.
You can't, not with unserialize. You can, however, throw something like this in there:
if sContents then tContents = textutils.unserialize(sContents) else tContents = {} end
function filereadline(filename, line) local file = io.open(filename, "r") sContents = file:read() -- capture file in a string file:close() if sContents then tContents = textutils.unserialize(sContents) -- convert string to table else tContents = "" end filetext = tContents(line) end
Error:
attempt to call string
MysticT 27 Jul 2012
It would be better to iterate through the table and write each line:
You can then use those functions like:
Note: you should try to understand the functions, not just copy them. If there's something you don't understand, just ask.
local function readLines(sPath) local file = fs.open(sPath, "r") if file then local tLines = {} local sLine = file.readLine() while sLine do table.insert(tLines, sLine) sLine = file.readLine() end file.close() return tLines end return nil end local function writeLines(sPath, tLines) local file = fs.open(sPath, "w") if file then for _, sLine in ipairs(tLines) do file.writeLine(sLine) end file.close() end end
You can then use those functions like:
local tLines = readLines("filename") print("Lines in the file: ", #tLines) tLines[2] = "This is line 2!" table.insert(tLines, "This is the last line!") writeLines("filename", tLines)
Note: you should try to understand the functions, not just copy them. If there's something you don't understand, just ask.
brett122798 27 Jul 2012
MysticT, on 27 July 2012 - 01:59 AM, said:
It would be better to iterate through the table and write each line:
You can then use those functions like:
Note: you should try to understand the functions, not just copy them. If there's something you don't understand, just ask.
local function readLines(sPath) local file = fs.open(sPath, "r") if file then local tLines = {} local sLine = file.readLine() while sLine do table.insert(tLines, sLine) sLine = file.readLine() end file.close() return tLines end return nil end local function writeLines(sPath, tLines) local file = fs.open(sPath, "w") if file then for _, sLine in ipairs(tLines) do file.writeLine(sLine) end file.close() end end
You can then use those functions like:
local tLines = readLines("filename") print("Lines in the file: ", #tLines) tLines[2] = "This is line 2!" table.insert(tLines, "This is the last line!") writeLines("filename", tLines)
Note: you should try to understand the functions, not just copy them. If there's something you don't understand, just ask.
Luanub 27 Jul 2012
Check and make sure the file exist and is able to be accessed by the program. I would add this to the start and see what it tells you.
Also when you declare tContents like
Another thing I would do this..
if not fs.exists("filename") then print ("File does not exists") end
Also when you declare tContents like
tContents = ""it is basically turning it into a string do
tContents = {}To declare it as a table.
Another thing I would do this..
filetext = tContents(line) -- change the () to []'s and add an if statement to check for no data if tContents[line] ~= nil then filetext = tContents[line] end
brett122798 27 Jul 2012
Yeah, the file doesn't exist but I only know how to create folders, not files. How can I get them to auto generate?
Luanub 27 Jul 2012
Do something like this to create an empty file
local file = io.open("filename", "w") file:write() file:close()
brett122798 27 Jul 2012
Okay, it's getting further along now. But I'm getting a new error! Yay!(Not really..) This has to do with my code though, it says:
attempt to concatenate nil and string
KingMachine 27 Jul 2012
It can't combine nil and a string somewhere in your code, probably into a string. Instead of variable.."string" try variable, "String". I believe is what can solve that. I don't think you want nil though, so the error is probably further up the code, specifically that whatever it thinks is nil isn't getting a proper assignment of it's value most likely.