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

[1.48 or 1.5] One little fs API function I would like...

Started by Shnupbups, 20 November 2012 - 12:34 PM
Shnupbups #1
Posted 20 November 2012 - 01:34 PM
The fs api is quite useful for things like keeping track of variables inside files, but if the file it's trying to open doesn't exist, it just returns nil. I think you should either have it create that file, or you could add:
fs.make(filename)
It would be useful for things like this:

if fs.exists("file") then
  file = fs.open("file","r")
  contents = file.readAll()
  file.close()
  print(contents)
else
  fs.make("file")
  file = fs.open("file","w")
  contents = "BlahBlah"
  file.write(contents)
  file.close()
  print(contents)
end
and I know I would use it a LOT!

Thanks for taking your time to read,
[indent=5]Shnup :(/>/>[/indent]

Also read my Advanced Printers suggestion!
Kingdaro #2
Posted 20 November 2012 - 02:01 PM
That's only the case if you're opening it for reading. To create a file, all you have to do is open it with writing mode.


fs.open('filename','w')

In terms of logic, though, it's definitely not really a straightforward solution.
Cloudy #3
Posted 20 November 2012 - 08:44 PM
Nope. The fs API closely mirrors the io API in terms of how it works. Pretty much every programming language works the same way - if opened with "w" it will create the file if not existing.
Espen #4
Posted 21 November 2012 - 12:10 AM
I think what might've happened is that Shnupbups100 has tried fs.open() on CCEmu.
Because when you try to do fs.open("something", "w") on a file that doesn't yet exist, CCEmu will return nil.
Cloudy #5
Posted 21 November 2012 - 12:20 AM
Yeah, either way it isn't our issue :(/>/>

The one time that fs.open("file", "w") will NOT make the file, is if it is in a folder which doesn't exist. And of course if there was an error making the file.