236 posts
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
445 posts
Posted 22 September 2013 - 09:46 AM
Supported by
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.
fopen modes
[…]
a - open for appending (file need not exist)
236 posts
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???
7508 posts
Location
Australia
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).
8543 posts
Posted 22 September 2013 - 01:18 PM
Moved to Ask a Pro.
445 posts
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
7508 posts
Location
Australia
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`
445 posts
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?
8543 posts
Posted 22 September 2013 - 02:18 PM
Append mode does already create files when used properly. So what are you talking about?
445 posts
Posted 22 September 2013 - 02:41 PM
Well it doesn't with the code I posted, what is improper about it?
1852 posts
Location
Sweden
Posted 22 September 2013 - 03:11 PM
Are you closing the file? If not then that can be a problem I think
8543 posts
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.
423 posts
Location
AfterLifeLochie's "Dungeon", Australia
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.
8543 posts
Posted 22 September 2013 - 08:05 PM
Oh! That would likely explain it. I hadn't realized it had been changed.
41 posts
Location
Everywhere
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
66 posts
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")
7083 posts
Location
Tasmania (AU)
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.