Jump to content




Append Mode To Create A File



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

#1 makerimages

  • Members
  • 236 posts

Posted 22 September 2013 - 08: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 by Lyqyd, 22 September 2013 - 02:30 PM.


#2 Pinkishu

  • Members
  • 484 posts

Posted 22 September 2013 - 08: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)


#3 makerimages

  • Members
  • 236 posts

Posted 22 September 2013 - 08: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???

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 September 2013 - 09:11 AM

View Postmakerimages, on 22 September 2013 - 08:48 AM, said:

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).

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 September 2013 - 12:18 PM

Moved to Ask a Pro.

#6 Pinkishu

  • Members
  • 484 posts

Posted 22 September 2013 - 12: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

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 September 2013 - 12:58 PM

View PostPinkishu, on 22 September 2013 - 12:24 PM, said:

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`

#8 Pinkishu

  • Members
  • 484 posts

Posted 22 September 2013 - 01:13 PM

View Posttheoriginalbit, on 22 September 2013 - 12:58 PM, said:

View PostPinkishu, on 22 September 2013 - 12:24 PM, said:

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?

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 September 2013 - 01:18 PM

Append mode does already create files when used properly. So what are you talking about?

#10 Pinkishu

  • Members
  • 484 posts

Posted 22 September 2013 - 01:41 PM

Well it doesn't with the code I posted, what is improper about it?

#11 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 22 September 2013 - 02:11 PM

Are you closing the file? If not then that can be a problem I think

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 September 2013 - 02: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.

#13 AfterLifeLochie

    Wiki Oracle

  • Moderators
  • 480 posts
  • LocationAfterLifeLochie's "Dungeon", Australia

Posted 22 September 2013 - 04:30 PM

View PostLyqyd, on 22 September 2013 - 02:29 PM, said:

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.

#14 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 September 2013 - 07:05 PM

Oh! That would likely explain it. I hadn't realized it had been changed.

#15 Imprivate

  • Members
  • 42 posts
  • LocationEverywhere

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 by Imprivate, 03 January 2014 - 11:53 AM.


#16 comp500

  • Members
  • 66 posts

Posted 25 April 2015 - 06:18 AM

View PostImprivate, on 03 January 2014 - 11:49 AM, said:

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")


#17 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 April 2015 - 06: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users