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

[Lua][Question] 2 things about saving to and loading files.

Started by TCGM, 08 July 2012 - 08:23 AM
TCGM #1
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:


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
Pinkishu #2
Posted 08 July 2012 - 01:07 PM
In loading you read line #1 when you do the nil-check
then the other readLines will be from line 2 onwards


In saving you could use append-mode instead of write-mode
TCGM #3
Posted 08 July 2012 - 01:38 PM
Thanks, I did manage to figure out the loading problem on my own after looking at this topic: http://www.computercraft.info/forums2/index.php?/topic/1404-fsopen-help/page__hl__%2Bsave+%2Bline+%2Bfile__fromsearch__1

What is the exact command for append mode?
Pinkishu #4
Posted 08 July 2012 - 02:00 PM
http://computercraft.info/wiki/index.php?title=Fs.open
"a" to open for writing but keep existing data and append any writes to the end of the file, plus optionally the second character
TCGM #5
Posted 09 July 2012 - 03:50 AM
the appending mode doesn't appear to be working, it still overwrites the file. Also, now something in my loading code is causing java to crash. Not the minecraft java, the java running on the computers. It's the loading code because if I remove it, the server runs fine.

Code:

print("Running eMail server")
n=0
a=0
local table1={}
local hHandle = fs.open("table1.txt", "r")
repeat
local line = hHandle.readLine()
print("Name: " .. line)
table.insert(table1, line)
print("Added name to database")
a = a+1
line2 = hHandle.readLine()
table1[line]=line2
print(line2)
print("Tied ID to name in database")
a = a+1
n = n+1
line = line2
until line ~= nil
hHandle.close()
Grim Reaper #6
Posted 09 July 2012 - 04:59 AM
In all honesty, I did not read your code as best I could have, however I think I may be able to offer some insight. Try and read the file into a string variable for temporary holding, then concatenate both the string holding the whole file with whatever you want to add and then write it back to the file.

Here is an example:


local file = fs.open("filePath", "r");
local sFileContents = file.readAll();
file.close();
-- The following n is a common escape sequence within strings that represent a new line, or line break
sFileContents = sFileContents.."n"..sFileAddtion; -- This will be the string holding whatever you wanted to add
file = fs.open("filePath", "w");
file.write(sFileContents);
file.close();

Please forgive my use of unnecessary semicolons for I have been doing a lot of Java programming as of late, and thus the OCD demon inside me refuses to let little errors like that slip :)/>/>

I hope this helps!
TCGM #7
Posted 09 July 2012 - 09:01 AM
In all honesty, I did not read your code as best I could have, however I think I may be able to offer some insight. Try and read the file into a string variable for temporary holding, then concatenate both the string holding the whole file with whatever you want to add and then write it back to the file.

Here is an example:


local file = fs.open("filePath", "r");
local sFileContents = file.readAll();
file.close();
-- The following n is a common escape sequence within strings that represent a new line, or line break
sFileContents = sFileContents.."n"..sFileAddtion; -- This will be the string holding whatever you wanted to add
file = fs.open("filePath", "w");
file.write(sFileContents);
file.close();

Please forgive my use of unnecessary semicolons for I have been doing a lot of Java programming as of late, and thus the OCD demon inside me refuses to let little errors like that slip :)/>/>

I hope this helps!

I'll have to work on implementing this, though it does look like it will accomplish what I need. Ill edit this post when Im done implementing it.

The major, major problem I have right now is the loading thing. It's CRASHING the CC java runtime!
Cloudy #8
Posted 09 July 2012 - 12:17 PM
Post the error? "crashing the java" isn't specific enough to see what's wrong.
TCGM #9
Posted 09 July 2012 - 12:29 PM
Post the error? "crashing the java" isn't specific enough to see what's wrong.

3/4 of the time, I can't see the error due to the FPS that MC drops to (1 per 5s), and when I did see it it merely said: "java.lang.NullPointerException()". Also, when this happens on a server, computercraft spams the console with "Failed to restart a computer. Hanging Lua thread might cause errors". Some of the other times all I get is the program exiting before it even began and giving me some random 3 digit number (362, 783, 299 etc).

EDIT: I've fixed the problem with the crash. There were a few ends, a few if's where they should not have been after I recently restructured the code.
DOUBLE EDIT: Append mode is now functional. Thanks!