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

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

Started by brett122798, 26 July 2012 - 04:54 AM
brett122798 #1
Posted 26 July 2012 - 06: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>
KingMachine #2
Posted 26 July 2012 - 07:12 AM
http://lua-users.org...LibraryTutorial
this is what you want. The forum cuts it off but it's the "IO" tutorial
brett122798 #3
Posted 26 July 2012 - 07:19 AM
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.
KingMachine #4
Posted 26 July 2012 - 08:18 AM
They don't write very well do they? It's the best source of info on the subject though sadly.
brett122798 #5
Posted 26 July 2012 - 08:51 AM
Somebody has got to know how to do this.
Luanub #6
Posted 26 July 2012 - 09: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.
brett122798 #7
Posted 26 July 2012 - 10:14 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.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! :)/>/>
brett122798 #8
Posted 27 July 2012 - 01: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?
Lyqyd #9
Posted 27 July 2012 - 01:17 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?

Make sure sContents isn't nil.
brett122798 #10
Posted 27 July 2012 - 01:23 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?

Make sure sContents isn't nil.
Oh, it is actually nothing. How can I get it to read nothing?
Lyqyd #11
Posted 27 July 2012 - 01:29 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?

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
brett122798 #12
Posted 27 July 2012 - 01:55 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?

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.
brett122798 #13
Posted 27 July 2012 - 02:48 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?

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
MysticT #14
Posted 27 July 2012 - 02: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.
brett122798 #15
Posted 27 July 2012 - 03:02 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.
I really don't like it that way, I just want my usual 'filereadline("file", 5)' Is there any fix for the error above?
Luanub #16
Posted 27 July 2012 - 04: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
brett122798 #17
Posted 27 July 2012 - 04: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?
Luanub #18
Posted 27 July 2012 - 04:15 AM
Do something like this to create an empty file


local file = io.open("filename", "w")
file:write()
file:close()
brett122798 #19
Posted 27 July 2012 - 04: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
KingMachine #20
Posted 27 July 2012 - 04: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.
brett122798 #21
Posted 27 July 2012 - 04:44 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.
Isn't nil nothing like:


test = ""
Like that?

Here's where I'm getting the error:


printCentred(math.floor(h/2) + 0, ((page2 == 1) and "> 1. "..user1.." <") or "1. "..user1)
So user1 is nil.
Luanub #22
Posted 27 July 2012 - 05:26 AM
How do you declare user1? Where does it obtain its value from? Might need to see more of the code to help with this one.
KingMachine #23
Posted 27 July 2012 - 05:28 AM
Yeah, just try defining "user1 = "test"" right above the first line that it's used. it'll work most likely
brett122798 #24
Posted 27 July 2012 - 05:32 AM
How do you declare user1? Where does it obtain its value from? Might need to see more of the code to help with this one.
Okay, it's my WIP OS so here's 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 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
filetext = tContents[line]
elseif tContents[line] ~= nil then
filetext = ""
end
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()
filereadline("mineosuserdatauser1", 1)
user1 = filetext
filereadline("mineosuserdatauser1", 2)
user1pass = filetext
filereadline("mineosuserdatauser2", 1)
user2 = filetext
filereadline("mineosuserdatauser2", 2)
user2pass = filetext
filereadline("mineosuserdatauser3", 1)
user3 = filetext
filereadline("mineosuserdatauser3", 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
foldercreate("mineos2/userdata")
filecreate("mineos2/userdata/user1")
filecreate("mineos2/userdata/user2")
filecreate("mineos2/userdata/user3")
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()
register()
drawregister()
elseif page1 == 3 then
os.reboot()
end
end
if key == 28 then
break
end
end

How can I fix the problem? Oh, and there's still a lot of flaws, I haven't programmed areas yet.
Luanub #25
Posted 27 July 2012 - 05:39 AM
I think it is due to this if statement in your filereadline function


if sContents then
tContents = textutils.unserialize(sContents) -- convert string to table
filetext = tContents[line]
elseif tContents[line] ~= nil then
filetext = ""
end



Try changing it to this:

if sContents then
tContents = textutils.unserialize(sContents) -- convert string to table
filetext = tContents[line]
else
filetext = ""
end


Also make sure there is data in the user1 file, just use test or something like that and then add a line print user1 so you can make sure it is setting it correctly.
brett122798 #26
Posted 27 July 2012 - 05:47 AM
I think it is due to this if statement in your filereadline function


if sContents then
tContents = textutils.unserialize(sContents) -- convert string to table
filetext = tContents[line]
elseif tContents[line] ~= nil then
filetext = ""
end



Try changing it to this:

if sContents then
tContents = textutils.unserialize(sContents) -- convert string to table
filetext = tContents[line]
else
filetext = ""
end


Also make sure there is data in the user1 file, just use test or something like that and then add a line print user1 so you can make sure it is setting it correctly.
IT WORKS FINALLY! YESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYESYES
brett122798 #27
Posted 27 July 2012 - 06:03 AM
Oh Great. Doesn't work when user1, user2, or user3 files have any text

Error:

mineos:40: attempt to call index ? (a nil value)
brett122798 #28
Posted 27 July 2012 - 07:28 AM
Really need help with this…
Luanub #29
Posted 27 July 2012 - 08:33 AM
I'll try playing with it some when I get home and see if I can't find the issue. I don't see anything obvious from just looking at the code.

You could setup a side script that just reads the files and prints the vars and see how that behaves.
brett122798 #30
Posted 27 July 2012 - 08:44 AM
I'll try playing with it some when I get home and see if I can't find the issue. I don't see anything obvious from just looking at the code.

You could setup a side script that just reads the files and prints the vars and see how that behaves.
Yeah.. but I'm pretty clueless to why it wouldn't work. Looks good to me.
Luanub #31
Posted 27 July 2012 - 11:14 AM
Okay I don't know why I didn't think about it earlier… the textutils commands need the string to be in a specific format or it won't convert it to a table.

Make sure the contents of your user files use the following formatting

{[1]="username",[2]="password",}
MysticT #32
Posted 27 July 2012 - 03:37 PM
That's why I said that it's better to read the lines into a table instead of using serialization. The serialization won't write each line as a separate line, it will write it as a single line, the serialized table (wich looks something like: {[1]="string", [2]="other string"}). You can change the functions I posted to do what you want, or even use them:

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

function filewriteline(filename, n, line)
  local tLines = readLines(filename)
  tLines[n] = line
  writeLines(filename, tLines)
end
brett122798 #33
Posted 27 July 2012 - 06:54 PM
That's why I said that it's better to read the lines into a table instead of using serialization. The serialization won't write each line as a separate line, it will write it as a single line, the serialized table (wich looks something like: {[1]="string", [2]="other string"}). You can change the functions I posted to do what you want, or even use them:

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

function filewriteline(filename, n, line)
  local tLines = readLines(filename)
  tLines[n] = line
  writeLines(filename, tLines)
end


mineos:35: attempt to call nil
EDIT: Wait, I may have screwed up on something, hold on.
EDIT: Nope, still same error.
MysticT #34
Posted 27 July 2012 - 07:59 PM
Code? I think you forgot to add the previous functions, or the name doesn't match.
brett122798 #35
Posted 27 July 2012 - 08:13 PM
Code? I think you forgot to add the previous functions, or the name doesn't match.
No, I replaced it and the name does match.
MysticT #36
Posted 27 July 2012 - 10:00 PM
Ok, can you post the code? It's impossible to know what's the error without looking at the code.
brett122798 #37
Posted 28 July 2012 - 12:25 AM
Ok, can you post the code? It's impossible to know what's the error without looking at the code.
It's the code you posted..
MysticT #38
Posted 28 July 2012 - 12:31 AM
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.
brett122798 #39
Posted 28 July 2012 - 01:01 AM
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.
Lyqyd #40
Posted 28 July 2012 - 01:04 AM
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.
brett122798 #41
Posted 28 July 2012 - 01:48 AM
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
MysticT #42
Posted 28 July 2012 - 03:07 AM
See, you didn't copied the first functions (readLines and writeLines), that's why it says they don't exist.
brett122798 #43
Posted 28 July 2012 - 03:21 AM
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)
MysticT #44
Posted 28 July 2012 - 08: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

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.
brett122798 #45
Posted 29 July 2012 - 12:50 AM
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

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?
MysticT #46
Posted 29 July 2012 - 12:54 AM
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?
brett122798 #47
Posted 29 July 2012 - 01:07 AM
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.
Lyqyd #48
Posted 29 July 2012 - 01:34 AM
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.
brett122798 #49
Posted 29 July 2012 - 01:41 AM
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.
Lyqyd #50
Posted 29 July 2012 - 03:14 AM
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.
brett122798 #51
Posted 29 July 2012 - 03:31 AM
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.
Tiin57 #52
Posted 29 July 2012 - 01:41 PM
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.
brett122798 #53
Posted 29 July 2012 - 06:18 PM
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.