Jump to content




[Lua]How to read/write certain lines in files?


52 replies to this topic

#1 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 26 July 2012 - 05:54 AM

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:

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>


#2 KingMachine

  • Members
  • 122 posts

Posted 26 July 2012 - 06:12 AM

http://lua-users.org...LibraryTutorial
this is what you want. The forum cuts it off but it's the "IO" tutorial

#3 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 26 July 2012 - 06:19 AM

View PostKingMachine, 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
You have no clue how long I had looked at that a few months ago and didn't get it.

#4 KingMachine

  • Members
  • 122 posts

Posted 26 July 2012 - 07:18 AM

They don't write very well do they? It's the best source of info on the subject though sadly.

#5 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 26 July 2012 - 07:51 AM

Somebody has got to know how to do this.

#6 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 26 July 2012 - 08:56 AM

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..

--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.

#7 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 26 July 2012 - 09:14 AM

View Postluanub, 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..

--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 this looks very good, I'll work on it tomorrow! Night.

Oh, and thanks a bunch! :)/>

#8 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 27 July 2012 - 12:04 AM

I'm getting an error when using this function I made:

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?

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,450 posts

Posted 27 July 2012 - 12:17 AM

View Postbrett122798, on 27 July 2012 - 12:04 AM, said:

I'm getting an error when using this function I made:

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.

#10 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 27 July 2012 - 12:23 AM

View PostLyqyd, on 27 July 2012 - 12:17 AM, said:

View Postbrett122798, on 27 July 2012 - 12:04 AM, said:

I'm getting an error when using this function I made:

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.
Oh, it is actually nothing. How can I get it to read nothing?

#11 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,450 posts

Posted 27 July 2012 - 12:29 AM

View Postbrett122798, on 27 July 2012 - 12:23 AM, said:

View PostLyqyd, on 27 July 2012 - 12:17 AM, said:

View Postbrett122798, on 27 July 2012 - 12:04 AM, said:

I'm getting an error when using this function I made:

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.
Oh, it is actually nothing. How can I get it to read nothing?

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


#12 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 27 July 2012 - 12:55 AM

View PostLyqyd, on 27 July 2012 - 12:29 AM, said:

View Postbrett122798, on 27 July 2012 - 12:23 AM, said:

View PostLyqyd, on 27 July 2012 - 12:17 AM, said:

View Postbrett122798, on 27 July 2012 - 12:04 AM, said:

I'm getting an error when using this function I made:

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.
Oh, it is actually nothing. How can I get it to read nothing?

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
Ah, ok. I'll test that out.

#13 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 27 July 2012 - 01:48 AM

View PostLyqyd, on 27 July 2012 - 12:29 AM, said:

View Postbrett122798, on 27 July 2012 - 12:23 AM, said:

View PostLyqyd, on 27 July 2012 - 12:17 AM, said:

View Postbrett122798, on 27 July 2012 - 12:04 AM, said:

I'm getting an error when using this function I made:

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.
Oh, it is actually nothing. How can I get it to read nothing?

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
Umm.. new error. Here's code:

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


#14 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 27 July 2012 - 01:59 AM

It would be better to iterate through the table and write each line:
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.

#15 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 27 July 2012 - 02:02 AM

View PostMysticT, on 27 July 2012 - 01:59 AM, said:

It would be better to iterate through the table and write each line:
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.
I really don't like it that way, I just want my usual 'filereadline("file", 5)' Is there any fix for the error above?

#16 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 27 July 2012 - 03:05 AM

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.

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


#17 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 27 July 2012 - 03:12 AM

Yeah, the file doesn't exist but I only know how to create folders, not files. How can I get them to auto generate?

#18 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 27 July 2012 - 03:15 AM

Do something like this to create an empty file

local file = io.open("filename", "w")
file:write()
file:close()


#19 brett122798

  • Members
  • 298 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 27 July 2012 - 03:32 AM

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


#20 KingMachine

  • Members
  • 122 posts

Posted 27 July 2012 - 03:36 AM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users