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

[Lua] [Question] How to write to a file, not overwrite

Started by Geforce Fan, 12 May 2013 - 06:46 PM
Geforce Fan #1
Posted 12 May 2013 - 08:46 PM
I'm doing log = fs.open("log", "ab") ;
log.write("(doesn't need to be specified)") ;
and each time I do that it overwrites. How do I make it not overwrite? What's the function instead of write?
nutcase84 #2
Posted 12 May 2013 - 08:59 PM
Like if you want to add lines? I don't know how to use the append thing, ask someone else. If you mean don't write it at all if that file exists, use fs.exists. Like this:

function fWrite(file, mode, data)
    if not fs.exists(file) then --this is what i'm talking about
		local f = fs.open(file, mode)
		f.write(data)
		f.close()
	    return true
    else
	    return false
	end
end

Hope this helps.

EDIT: Sorry about the indenting, my keyboard is screwed up.
KaoS #3
Posted 12 May 2013 - 09:19 PM
I'm doing log = fs.open("log", "ab") ;
log.write("(doesn't need to be specified)") ;
and each time I do that it overwrites. How do I make it not overwrite? What's the function instead of write?

why not just fs.open("log", "a")
Geforce Fan #4
Posted 12 May 2013 - 09:21 PM
Like if you want to add lines? I don't know how to use the append thing, ask someone else. If you mean don't write it at all if that file exists, use fs.exists. Like this:

function fWrite(file, mode, data)
	if not fs.exists(file) then --this is what i'm talking about
		local f = fs.open(file, mode)
		f.write(data)
		f.close()
		return true
	else
		return false
	end
end

Hope this helps.

EDIT: Sorry about the indenting, my keyboard is screwed up.
I mean write, let's say I have a file I'm writting to called log. Log already contains "lol xD".
I then want to add "Hi." to that
But not just doing write("lol xD Hi."), I mean ADDING to it without overwriting ANYTHING on that file.



I'm doing log = fs.open("log", "ab") ;
log.write("(doesn't need to be specified)") ;
and each time I do that it overwrites. How do I make it not overwrite? What's the function instead of write?

why not just fs.open("log", "a")
Does it matter?
KaoS #5
Posted 12 May 2013 - 09:45 PM
Does it matter?

Not to be rude but pointless items in code is unprofessional, a code should be short, fast and effective. If it contains unnecessary things it is harder to troubleshoot, it probably won't make a difference to your issue, it was just an observation.
Geforce Fan #6
Posted 12 May 2013 - 09:55 PM
Does it matter?

Not to be rude but pointless items in code is unprofessional, a code should be short, fast and effective. If it contains unnecessary things it is harder to troubleshoot, it probably won't make a difference to your issue, it was just an observation.
But still, if you don't know my problem why post here?
Kingdaro #7
Posted 12 May 2013 - 09:58 PM
That is your problem. Using "a" instead of "w" will append to the file.

You're using "ab", which is append binary mode, and expects numbers instead of strings, where the number can range from 0 to 255, and matches up with a specific character.

This number range relates to the use of string.char and string.byte, where string.char(num) returns the character for a specific byte (number between 0 and 255) and string.byte(char) returns the number for a specific character.
Geforce Fan #8
Posted 12 May 2013 - 10:22 PM
Okay, one last question:
How do I make a new line in the file?
Edit:
This still overwrites what's in the file
Edit:
Was able to figure the overwriting out on my own, but I still need to know how to make a new line, i need something humanly readable.
Kingdaro #9
Posted 13 May 2013 - 12:22 AM
Use file.writeLine.

file = fs.open("some file", "a")
file.writeLine("a line")
file.writeLine("another line")
file.writeLine("yet another line")
file.close()

The content of "some file":

a line
another line
yet another line
theoriginalbit #10
Posted 13 May 2013 - 03:22 AM
But still, if you don't know my problem why post here?
Well firstly its a little thing called trying to help people learn better ways to do things. Also when code is written better it really DOES make it easier to debug a program! For both you and us when you come to us for help.
Secondly, the problem actually is what KaoS was asking about, it seems that append-binary mode overwrites the entire file contents, I suspect this could be a bug. However removing the 'b' will fix the problem and the log will be appended to the end of the file… why were you wanting to use binary mode anyway?