Jump to content




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


  • You cannot reply to this topic
9 replies to this topic

#1 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

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?

#2 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

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:
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 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 12 May 2013 - 08:19 PM

View PostIcanbreathecode, 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?

why not just fs.open("log", "a")

#4 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 12 May 2013 - 08:21 PM

View Postnutcase84, 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:
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.



View PostKaoS, on 12 May 2013 - 08:19 PM, said:

View PostIcanbreathecode, 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?

why not just fs.open("log", "a")
Does it matter?

#5 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 12 May 2013 - 08:45 PM

View PostIcanbreathecode, 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 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 12 May 2013 - 08:55 PM

View PostKaoS, on 12 May 2013 - 08:45 PM, said:

View PostIcanbreathecode, 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.
But still, if you don't know my problem why post here?

#7 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

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.

#8 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

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.

#9 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 12 May 2013 - 11:22 PM

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


#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 May 2013 - 02:22 AM

View PostIcanbreathecode, on 12 May 2013 - 08:55 PM, said:

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?





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users