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

fs.open("filename", "a") Question

Started by tfoote, 24 June 2012 - 02:23 PM
tfoote #1
Posted 24 June 2012 - 04:23 PM
When using fs.open("filename", "a") do you start writing at the end of the file? I am creating an e-mail server and this is my code so far…
Spoiler

function escape()
term.clear()
term.setCursorPos(1,1)
print("MENU:")
print("1.   Send")
print("2.   Discard & Quit")
print("3.   Back")
print("Press 1, 2, or 3)
local event, esc = os.pullEvent("char")
if esc == 1 then
type = 2
send()
elseif esc == 2 then
type = 2
elseif esc == 3 then
type = 1
erm.clear()
term.setCursorPos(1,1)
print("Please Type Body")
print("To Exit Push Escape")
print(read.readAll())
read.close()
else
print("wrong key")
type = 1
term.clear()
term.setCursorPos(1,1)
print("Please Type Body")
print("To Exit Push Escape")
print(read.readAll())
read.close()
end
end
print("Please Type Body")
print("To Exit push Escape")
local read = fs.open("body". "r")
local append = fs.open("body", "a")
local write = fs.open("body", "w")
term.setCursorBlink(true)
local event, key = os.pullEvent("char")
if key == "escape" then
escape()
else
write.write(key)
write(key)
type = 1
if type == 1 then
local event, key = os.pullEvent("char")
if key == "escape" then
escape()
else
append.write(key)
write(key)
end
end
MysticT #2
Posted 24 June 2012 - 04:52 PM
From the wiki:
"a" to open for writing but keep existing data and append any writes to the end of the file
Is it so hard to go to the wiki and read a little?
tfoote #3
Posted 24 June 2012 - 04:53 PM
I went there… But it didn't say weather it wrote to annother line. That is my problem. I want to know if it continues where it left of or starts a new line
MysticT #4
Posted 24 June 2012 - 04:55 PM
It writes to the end of the file, so if there was a newline character at the end it will start in the next line, otherwise it keeps writing in the same line.
tfoote #5
Posted 24 June 2012 - 05:01 PM
Thank you
Bossman201 #6
Posted 25 June 2012 - 12:14 AM
Try:
file.writeLine(message)


It writes to the end of the file, so if there was a newline character at the end it will start in the next line, otherwise it keeps writing in the same line.
Also, the newline character is "n".