Posted 08 July 2012 - 10:23 AM
Hey there! Annoying old me again :)/>/>
Anyways, I've moved on to the next step in my email program set. I've gotten loading and saving down pat, except for two minor glitches.
1. Saving appears to overwrite the file each time, instead of merely adding an entry into it.
2. Loading, for some reason, doesnt want to start at line 1, but instead starts at line 2 of the text file.
I.E. program says to scan line one, file is set up like:
Name
ID
Name
ID
Name
ID
the program starts at line 2, ID, then continues from there. So it's loaded database goes:
ID
Name
ID
Name
ID
Note, all of this is server-side, the client has nothing to do with it.
Code for Loading:
Code for Saving:
Anyways, I've moved on to the next step in my email program set. I've gotten loading and saving down pat, except for two minor glitches.
1. Saving appears to overwrite the file each time, instead of merely adding an entry into it.
2. Loading, for some reason, doesnt want to start at line 1, but instead starts at line 2 of the text file.
I.E. program says to scan line one, file is set up like:
Name
ID
Name
ID
Name
ID
the program starts at line 2, ID, then continues from there. So it's loaded database goes:
ID
Name
ID
Name
ID
Note, all of this is server-side, the client has nothing to do with it.
Code for Loading:
print("Running eMail server")
n=0
a=0
local table1={}
local hHandle = fs.open("table1.txt", "r")
repeat
if hHandle:readLine(a) == nil then
print("Database Empty")
else
local line = tostring(hHandle:readLine(a))
print(line)
table.insert(table1, line)
print("Added name to database")
a = a+1
line2 = hHandle:readLine(a)
table1[line]=line2
print(line2)
print("Tied ID to name in database")
a = a+1
n = n+1
end
until line == nil
hHandle.close()
Code for Saving:
if message == "register" then
id, regname = rednet.receive()
rednet.broadcast("reged")
table.insert(table1,regname)
table1[regname]=id
print("Computer " .. table1[regname])
print(" registered as name " .. table1[n+1])
print(table.maxn(table1))
local file = fs.open("table1.txt", "w") -- open the file
if file then -- check if it's open
local s = tostring(table1[n+1]) -- serialize the table, so we have a string to save to the file
file.writeLine(s) -- write it to the file
local s = tostring(table1[regname]) -- serialize the table, so we have a string to save to the file
print("Printing S" .. s)
file.writeLine(s) -- write it to the file
file.close() -- always close the file handles
end