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

Append Mode To Create A File

Started by makerimages, 22 September 2013 - 07:34 AM
makerimages #1
Posted 22 September 2013 - 09:34 AM
Currently, only
 "w"
mode creates a file when it is not existing, could we get

"a"
to do the same, please?

[Note from Lyqyd: This is a bug that cropped up somewhere between 1.53 and 1.56. -L]
Edited on 22 September 2013 - 01:30 PM
Pinkishu #2
Posted 22 September 2013 - 09:46 AM
Supported by
Lua 5.1 Reference Manual said:
The mode string can also have a 'b' at the end, which is needed in some systems to open the file in binary mode. This string is exactly what is used in the standard C function fopen.
Various Sources said:
fopen modes
[…]
a - open for appending (file need not exist)
makerimages #3
Posted 22 September 2013 - 09:48 AM
yet when I do that, file is not created, do I need to open, close, open,write, close to be able to write???
theoriginalbit #4
Posted 22 September 2013 - 10:11 AM
yet when I do that, file is not created, do I need to open, close, open,write, close to be able to write???
Ok so how file handles work is you open the handle. Each write is written to a buffer, this buffer is not the file, it is not until you flush the buffer or close the handle (which flushes and closes) that the text is actually written to the file (this is where the file is created if it doesn't exist).
Lyqyd #5
Posted 22 September 2013 - 01:18 PM
Moved to Ask a Pro.
Pinkishu #6
Posted 22 September 2013 - 01:24 PM
Uh, this isn't "Ask a Pro", it's a suggestion to make CC create a file when trying to open a non-existent file in "a" mode


f = fs.open("blahhh","a")
if f then f.close() else print("no file") end

will print "no file" if "blahhh" doesn't exist; but if you use "w" mode, it will create the file
"a" should create the file too though
theoriginalbit #7
Posted 22 September 2013 - 01:58 PM
Uh, this isn't "Ask a Pro", it's a suggestion to make CC create a file when trying to open a non-existent file in "a" mode
But since you can already do that, it moves from the realm of `suggestion` to the realm of "poster doesn't know how to do it" ergo, `Ask a Pro`
Pinkishu #8
Posted 22 September 2013 - 02:13 PM
Uh, this isn't "Ask a Pro", it's a suggestion to make CC create a file when trying to open a non-existent file in "a" mode
But since you can already do that, it moves from the realm of `suggestion` to the realm of "poster doesn't know how to do it" ergo, `Ask a Pro`

Yeah you can create files, but why not follow Lua Reference Manual standards?
Lyqyd #9
Posted 22 September 2013 - 02:18 PM
Append mode does already create files when used properly. So what are you talking about?
Pinkishu #10
Posted 22 September 2013 - 02:41 PM
Well it doesn't with the code I posted, what is improper about it?
TheOddByte #11
Posted 22 September 2013 - 03:11 PM
Are you closing the file? If not then that can be a problem I think
Lyqyd #12
Posted 22 September 2013 - 03:29 PM
Tested in 1.56 and confirmed as a bug. This is a regression from previous versions. File handles in append mode are known to create files properly in 1.53.
AfterLifeLochie #13
Posted 22 September 2013 - 05:30 PM
Tested in 1.56 and confirmed as a bug. This is a regression from previous versions. File handles in append mode are known to create files properly in 1.53.

This isn't regression, this likely happened when Dan changed the filesystem implementation in 1.56.
Lyqyd #14
Posted 22 September 2013 - 08:05 PM
Oh! That would likely explain it. I hadn't realized it had been changed.
Imprivate #15
Posted 03 January 2014 - 11:49 AM
Using CC 1.58 and it still hasn't been fixed :/ VERY annoying. I'm trying to check wether a file exists, and if it doesn't, create one. Only way I can think of is open it in "w" first, close it then open i again in "a". :/
Edited on 03 January 2014 - 10:53 AM
comp500 #16
Posted 25 April 2015 - 07:18 AM
Using CC 1.58 and it still hasn't been fixed :/ VERY annoying. I'm trying to check wether a file exists, and if it doesn't, create one. Only way I can think of is open it in "w" first, close it then open i again in "a". :/
sorry for the necropost, but just if someone stumbles across this…

The wiki says this:

Opens "abcd" in either append or write mode, depending on whether it already exists or not.

local h = fs.open("abcd", fs.exists("abcd") and "a" or "w")
Bomb Bloke #17
Posted 25 April 2015 - 07:23 AM
FWIW you no longer need to do that with the most recent CC versions. Beats me when the behaviour changed back again, but those wishing to publish their scripts are probably still better off leaving that compatibility tweak in there.