1029 posts
Location
Missouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension
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?
770 posts
Location
In My Lonely Little Computer Corner
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.
1548 posts
Location
That dark shadow under your bed...
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")
1029 posts
Location
Missouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension
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?
1548 posts
Location
That dark shadow under your bed...
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.
1029 posts
Location
Missouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension
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?
1688 posts
Location
'MURICA
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.
1029 posts
Location
Missouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension
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.
1688 posts
Location
'MURICA
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
7508 posts
Location
Australia
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?