[Lua] [Question] How to write to a file, not overwrite
Started by Geforce Fan, May 12 2013 07:46 PM
9 replies to this topic
#1
Posted 12 May 2013 - 07: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?
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?
#2
Posted 12 May 2013 - 07: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:
Hope this helps.
EDIT: Sorry about the indenting, my keyboard is screwed up.
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.
#3
Posted 12 May 2013 - 08:19 PM
Icanbreathecode, on 12 May 2013 - 07:46 PM, said:
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?
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")
#4
Posted 12 May 2013 - 08:21 PM
nutcase84, on 12 May 2013 - 07:59 PM, said:
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:
Hope this helps.
EDIT: Sorry about the indenting, my keyboard is screwed up.
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 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.
KaoS, on 12 May 2013 - 08:19 PM, said:
Does it matter?
#5
Posted 12 May 2013 - 08:45 PM
Icanbreathecode, on 12 May 2013 - 08:21 PM, said:
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.
#6
Posted 12 May 2013 - 08:55 PM
KaoS, on 12 May 2013 - 08:45 PM, said:
Icanbreathecode, on 12 May 2013 - 08:21 PM, said:
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.
#7
Posted 12 May 2013 - 08: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.
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.
#8
Posted 12 May 2013 - 09: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.
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.
#9
Posted 12 May 2013 - 11:22 PM
Use file.writeLine.
The content of "some file":
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
#10
Posted 13 May 2013 - 02:22 AM
Icanbreathecode, on 12 May 2013 - 08:55 PM, said:
But still, if you don't know my problem why post here?
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?
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











