Jump to content




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


52 replies to this topic

#41 brett122798

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

Posted 28 July 2012 - 12:48 AM

View PostLyqyd, on 28 July 2012 - 12:04 AM, said:

View Postbrett122798, on 28 July 2012 - 12:01 AM, said:

View PostMysticT, on 27 July 2012 - 11:31 PM, said:

I know, but I need to see the whole code, or at least the part that has it. I don't know what line 35 is, so is kinda hard to see the error.
Look further up in the page.

You could have already posted the code in the time it took you to argue about posting the code. If you don't post the code, it's impossible to think of and rule out all of the myriad interactions that might cause problems. We also have no reference for what line 35 might be. Post the code.
os.pullEvent = os.pullEventRaw

function filecreate(path)
local file = io.open(path, "a")
file:write()
file:close()
end

function foldercreate(dir)
fs.makeDir(dir)
end

function filedelete(dir)
fs.delete(dir)
end

function filecopy(from, to)
fs.copy(from, to)
end

function filemove(from, to)
fs.move(from, to)
end

function filewriteline(filename, line, text)
local file = io.open(filename, "r")
sContents = file:read() -- capture file in a string
file:close()
tContents = textutils.unserialize(sContents) -- convert string to table
table.insert(tContents, line, text)
end

function filereadline(filename, line)
  local filetext = readLines(filename)
  return filetext[line]
end

function filedeleteline(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
table.remove(tContents, line)
end

function filesize(dir)
size = fs.getSize(dir)
end

w, h = term.getSize()

function printCentred( y, s )
    local x = math.floor((w - string.len(s)) / 2)
    term.setCursorPos(x,y)
    term.clearLine()
    term.write( s )
    end
    
function drawregister()
foldercreate("mineos2/userdata")
filecreate("mineos2/userdata/user1")
filecreate("mineos2/userdata/user2")
filecreate("mineos2/userdata/user3")
filereadline("mineos2/userdata/user1", 1)
user1 = filetext
filereadline("mineos2/userdata/user1", 2)
user1pass = filetext
filereadline("mineos2/userdata/user2", 1)
user2 = filetext
filereadline("mineos2/userdata/user2", 2)
user2pass = filetext
filereadline("mineos2/userdata/user3", 1)
user3 = filetext
filereadline("mineos2/userdata/user3", 2)
user3pass = filetext
printCentred(math.floor(h/2) - 3, "---------------------")
printCentred(math.floor(h/2) - 2, "|  MineOS Register  |")
printCentred(math.floor(h/2) - 1, "---------------------")
if user1 == "" then
printCentred(math.floor(h/2) + 0, ((page2 == 1) and "> 1. <Empty> <") or "1. <Empty>")
else
printCentred(math.floor(h/2) + 0, ((page2 == 1) and "> 1. "..user1.." <") or "1. "..user1)
end
if user2 == "" then
printCentred(math.floor(h/2) + 1, ((page2 == 2) and "> 2. <Empty> <") or "2. <Empty>")
else
printCentred(math.floor(h/2) + 1, ((page2 == 2) and "> 1. "..user2.." <") or "1. "..user2)
end
if user3 == "" then
printCentred(math.floor(h/2) + 2, ((page2 == 3) and "> 3. <Empty> <") or "3. <Empty>")
else
printCentred(math.floor(h/2) + 2, ((page2 == 3) and "> 1. "..user3.." <") or "1. "..user3)
end
end

function register()
page2 = 1
while true do
local e,key = os.pullEventRaw("key")
if key == 17 or key == 200 then
if page2 > 1 then
page2 = page2 - 1
drawregister()
end
elseif key == 31 or key == 208 then
if page2 < 3 then
page2 = page2 + 1
drawregister()
end
elseif key == 28 then
break
end
if page2 == "1" then

elseif page2 == "2" then
register()
drawregister()
elseif page3 == "3" then
os.shutdown()
end
end
end

function drawmainmenu()
printCentred(math.floor(h/2) - 2, "MineOS Version 0.X")
printCentred(math.floor(h/2) - 1, "" )
printCentred(math.floor(h/2) + 0, ((page1 == 1) and "> Login <") or "Login")
printCentred(math.floor(h/2) + 1, ((page1 == 2) and "> Register <") or "Register")
printCentred(math.floor(h/2) + 2, ((page1 == 3) and "> Shutdown <") or "Shutdown")
printCentred(math.floor(h/2) + 3, "" )
end

print("Starting MineOS...")
print("")
sleep(1)
i = 0
repeat
i = i+1
print("Progress: "..i.."%")
sleep(0.04)
until i == 100
sleep(0.4)
term.clear()
page1 = 1
drawmainmenu()
while true do
local e,key = os.pullEventRaw("key")
if key == 17 or key == 200 then
if page1 > 1 then
page1 = page1 - 1
drawmainmenu()
end
elseif key == 31 or key == 208 then
if page1 < 3 then
page1 = page1 + 1
drawmainmenu()
end
elseif key == 28 then
if page1 == 1 then
elseif page1 == 2 then
term.clear()
drawregister()
register()
elseif page1 == 3 then
os.reboot()
end
end
if key == 28 then
break
end
end


#42 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 28 July 2012 - 02:07 AM

See, you didn't copied the first functions (readLines and writeLines), that's why it says they don't exist.

#43 brett122798

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

Posted 28 July 2012 - 02:21 AM

View PostMysticT, on 28 July 2012 - 02:07 AM, said:

See, you didn't copied the first functions (readLines and writeLines), that's why it says they don't exist.
Okay, I'm very clueless to what you're saying so can you give me my code with the functions correctly installed. I also need the text, when read, to be called "filetext."

Examples:

Read
filereadline("test/textfile", 5)
print(filetext)

Write:
sometext = "test"
filewriteline("test/textfile", 5, sometext)


#44 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 28 July 2012 - 07:06 PM

Ok, here's some explanations of the functions:
local function readLines(sPath)
  local file = fs.open(sPath, "r") -- open the file
  if file then -- check if it's open
    local tLines = {} -- table to store the lines
    local sLine = file.readLine() -- read a line from the file
    while sLine do -- while there's a line in the file
	  table.insert(tLines, sLine) -- add it to the table
	  sLine = file.readLine() -- get the next line
    end
    file.close() -- close the file
    return tLines -- return the table with the lines
  end
  return nil -- there was an error opening the file, return nil to let the user know
end

local function writeLines(sPath, tLines)
  local file = fs.open(sPath, "w") -- open the file
  if file then -- check if the file is open
    for _, sLine in ipairs(tLines) do -- for each line in the table
	  file.writeLine(sLine) -- write the line
    end
    file.close() -- close the file
  end
end

Now, you can use them in your code, but you need to add them (copy them before the functions that use them). So your functions would look like:
-- copy the functions here

function filereadline(filename, line)
  local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  if not tLines then -- if there was an error
    return nil -- return nil/error
  end
  return tLines[line] -- return the line
end

function filewriteline(filename, line, text)
  local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  if tLines then -- if there was no error
    tLines[line] = text -- set the line
    writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  end
end

View Postbrett122798, on 28 July 2012 - 02:21 AM, said:

I also need the text, when read, to be called "filetext."

Examples:

Read
filereadline("test/textfile", 5)
print(filetext)

Write:
sometext = "test"
filewriteline("test/textfile", 5, sometext)
Global variables? No thanks.

#45 brett122798

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

Posted 28 July 2012 - 11:50 PM

View PostMysticT, on 28 July 2012 - 07:06 PM, said:

Ok, here's some explanations of the functions:
local function readLines(sPath)
  local file = fs.open(sPath, "r") -- open the file
  if file then -- check if it's open
	local tLines = {} -- table to store the lines
	local sLine = file.readLine() -- read a line from the file
	while sLine do -- while there's a line in the file
	  table.insert(tLines, sLine) -- add it to the table
	  sLine = file.readLine() -- get the next line
	end
	file.close() -- close the file
	return tLines -- return the table with the lines
  end
  return nil -- there was an error opening the file, return nil to let the user know
end

local function writeLines(sPath, tLines)
  local file = fs.open(sPath, "w") -- open the file
  if file then -- check if the file is open
	for _, sLine in ipairs(tLines) do -- for each line in the table
	  file.writeLine(sLine) -- write the line
	end
	file.close() -- close the file
  end
end

Now, you can use them in your code, but you need to add them (copy them before the functions that use them). So your functions would look like:
-- copy the functions here

function filereadline(filename, line)
  local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  if not tLines then -- if there was an error
	return nil -- return nil/error
  end
  return tLines[line] -- return the line
end

function filewriteline(filename, line, text)
  local tLines = readLines(filename) -- read the lines from the file (using the previous functions)
  if tLines then -- if there was no error
	tLines[line] = text -- set the line
	writeLines(filename, tLines) -- write the lines back to the file (using the previous functions)
  end
end

View Postbrett122798, on 28 July 2012 - 02:21 AM, said:

I also need the text, when read, to be called "filetext."

Examples:

Read
filereadline("test/textfile", 5)
print(filetext)

Write:
sometext = "test"
filewriteline("test/textfile", 5, sometext)
Global variables? No thanks.
...

That's the way I need it, I really need the text of a file's line to be read with "filetext." Please?

#46 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 28 July 2012 - 11:54 PM

I don't like using global variables, it's bad practice. Why do you need it that way?
Also, if you can't do it yourself (modify the code to use the global variable), why do you think you can code an os?

#47 brett122798

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

Posted 29 July 2012 - 12:07 AM

View PostMysticT, on 28 July 2012 - 11:54 PM, said:

I don't like using global variables, it's bad practice. Why do you need it that way?
Also, if you can't do it yourself (modify the code to use the global variable), why do you think you can code an os?
I don't understand how it's such a problem, and also, would filereadline read lines with no text? I need it to read no text without errors and junk.

#48 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 29 July 2012 - 12:34 AM

View Postbrett122798, on 29 July 2012 - 12:07 AM, said:

View PostMysticT, on 28 July 2012 - 11:54 PM, said:

I don't like using global variables, it's bad practice. Why do you need it that way?
Also, if you can't do it yourself (modify the code to use the global variable), why do you think you can code an os?
I don't understand how it's such a problem, and also, would filereadline read lines with no text? I need it to read no text without errors and junk.

You're putting in very little effort and demanding quite a bit. You've been given good code that solves your problem numerous times and it is only through your own lack of understanding and unwillingness to adapt either what you already have or what you've been given that you still have problems. You cannot expect us to extensively study the rest of your code and come up with a solution that works with the bad coding practices already in place such that you can just cut and paste it in and expect things to work. You come to us with broken code for an OS (typically not a simple venture), so we expect you to be able to integrate a well-coded solution yourself. That's why it's such a problem.

#49 brett122798

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

Posted 29 July 2012 - 12:41 AM

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

View Postbrett122798, on 29 July 2012 - 12:07 AM, said:

View PostMysticT, on 28 July 2012 - 11:54 PM, said:

I don't like using global variables, it's bad practice. Why do you need it that way?
Also, if you can't do it yourself (modify the code to use the global variable), why do you think you can code an os?
I don't understand how it's such a problem, and also, would filereadline read lines with no text? I need it to read no text without errors and junk.

You're putting in very little effort and demanding quite a bit. You've been given good code that solves your problem numerous times and it is only through your own lack of understanding and unwillingness to adapt either what you already have or what you've been given that you still have problems. You cannot expect us to extensively study the rest of your code and come up with a solution that works with the bad coding practices already in place such that you can just cut and paste it in and expect things to work. You come to us with broken code for an OS (typically not a simple venture), so we expect you to be able to integrate a well-coded solution yourself. That's why it's such a problem.
All I asked for was simple filereadline and filewriteline functions with certain things in them, this has got to be simple to some people, that is the point of this forum.

#50 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 29 July 2012 - 02:14 AM

View Postbrett122798, on 29 July 2012 - 12:41 AM, said:

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

View Postbrett122798, on 29 July 2012 - 12:07 AM, said:

View PostMysticT, on 28 July 2012 - 11:54 PM, said:

I don't like using global variables, it's bad practice. Why do you need it that way?
Also, if you can't do it yourself (modify the code to use the global variable), why do you think you can code an os?
I don't understand how it's such a problem, and also, would filereadline read lines with no text? I need it to read no text without errors and junk.

You're putting in very little effort and demanding quite a bit. You've been given good code that solves your problem numerous times and it is only through your own lack of understanding and unwillingness to adapt either what you already have or what you've been given that you still have problems. You cannot expect us to extensively study the rest of your code and come up with a solution that works with the bad coding practices already in place such that you can just cut and paste it in and expect things to work. You come to us with broken code for an OS (typically not a simple venture), so we expect you to be able to integrate a well-coded solution yourself. That's why it's such a problem.
All I asked for was simple filereadline and filewriteline functions with certain things in them, this has got to be simple to some people, that is the point of this forum.

Did you read anything I said? You were provided workable functions that would have been easily adapted to your existing code. Instead of putting in the minimal effort to understand them and modify them to work for your program (which would have been a good thing for your program anyway), you treated them as a black box and when they didn't work, threw them out and asked for the same thing again, a thing which you were already given. You should not be surprised when the people whose work you threw out (despite it working fine, with the only issue being on your end) are then unwilling to help you, nor should you be surprised when other people in a position to help observe this behavior and are reticent to help.

#51 brett122798

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

Posted 29 July 2012 - 02:31 AM

View PostLyqyd, on 29 July 2012 - 02:14 AM, said:

View Postbrett122798, on 29 July 2012 - 12:41 AM, said:

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

View Postbrett122798, on 29 July 2012 - 12:07 AM, said:

View PostMysticT, on 28 July 2012 - 11:54 PM, said:

I don't like using global variables, it's bad practice. Why do you need it that way?
Also, if you can't do it yourself (modify the code to use the global variable), why do you think you can code an os?
I don't understand how it's such a problem, and also, would filereadline read lines with no text? I need it to read no text without errors and junk.

You're putting in very little effort and demanding quite a bit. You've been given good code that solves your problem numerous times and it is only through your own lack of understanding and unwillingness to adapt either what you already have or what you've been given that you still have problems. You cannot expect us to extensively study the rest of your code and come up with a solution that works with the bad coding practices already in place such that you can just cut and paste it in and expect things to work. You come to us with broken code for an OS (typically not a simple venture), so we expect you to be able to integrate a well-coded solution yourself. That's why it's such a problem.
All I asked for was simple filereadline and filewriteline functions with certain things in them, this has got to be simple to some people, that is the point of this forum.

Did you read anything I said? You were provided workable functions that would have been easily adapted to your existing code. Instead of putting in the minimal effort to understand them and modify them to work for your program (which would have been a good thing for your program anyway), you treated them as a black box and when they didn't work, threw them out and asked for the same thing again, a thing which you were already given. You should not be surprised when the people whose work you threw out (despite it working fine, with the only issue being on your end) are then unwilling to help you, nor should you be surprised when other people in a position to help observe this behavior and are reticent to help.
I just asked for these functions to work a very certain way but then I get something quite different from what I wanted. If it isn't possible, just tell me, why does it have to be such a pain? If you don't want to help, I'll just go elsewhere.

#52 Tiin57

    Java Lunatic

  • Members
  • 1,412 posts
  • LocationIndiana, United States

Posted 29 July 2012 - 12:41 PM

View Postbrett122798, on 29 July 2012 - 02:31 AM, said:

If you don't want to help, I'll just go elsewhere.
I forgot there was another CC forum. :ph34r:/>
Back to the point: You need to do this:
local filetext = fileReadLine("test/textfile", 5)
print(filetext)
I think that's right. Could be.
Edit: Also, using local variables in no way inhibits your code unless you plan to reuse the variables in another program. Do it. Otherwise, conflicts and nasty things are possible. Especially in something as complex as an OS.

#53 brett122798

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

Posted 29 July 2012 - 05:18 PM

View Posttiin57, on 29 July 2012 - 12:41 PM, said:

View Postbrett122798, on 29 July 2012 - 02:31 AM, said:

If you don't want to help, I'll just go elsewhere.
I forgot there was another CC forum. :ph34r:/>
Back to the point: You need to do this:
local filetext = fileReadLine("test/textfile", 5)
print(filetext)
I think that's right. Could be.
Edit: Also, using local variables in no way inhibits your code unless you plan to reuse the variables in another program. Do it. Otherwise, conflicts and nasty things are possible. Especially in something as complex as an OS.
Oh, so that's what a global variable is... and I'll insert that code there.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users