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

fs.open() modes?

Started by digpoe, 19 October 2012 - 12:41 PM
digpoe #1
Posted 19 October 2012 - 02:41 PM
I typed 'help fs' as I did not know what it was, but at the end it has fs.open() modes are [modes]

I don't know what these modes are, could someone tell me what they are?

EDIT: I found what they meant.. Lua 5.1 reference manual. Lol. but the ones with the 'b' on the end still don't make sense.
Espen #2
Posted 19 October 2012 - 02:51 PM
The mode denotes how you want to open the file, i.e. if you want to read it, write to it, append to the end of it, etc.
Basically you should be able to use the same modes as described here:
http://www.lua.org/m...tml#pdf-io.open

So e.g. if you'd wanted to open a file for reading in ComputerCraft, you'd use something like…
local hFile = fs.open( "filename", "r" )
… where r stands for read.
digpoe #3
Posted 19 October 2012 - 02:54 PM
Thanks. :)/>/>
I was wondering how I would read files too :P/>/>

so, if I wanted to read the words "Hello, world" from a file named "filedata" in a disk in the bottom drive.. wait. I don't know how.. I'm going to guess this?:

local diskdata = ""
sleep(5)
diskdata = fs.open(disk.getMountPath("bottom")["filedata"], "r")

if I'm wrong, please tell me how I would do it.
Espen #4
Posted 19 October 2012 - 03:01 PM
No problem.
Just noticed your edit. The "b" stands for binary mode and allows one to write data to the file in the form of bytes instead of characters/text.
More on writing in binary mode: http://www.lua.org/pil/21.2.2.html
digpoe #5
Posted 19 October 2012 - 03:17 PM
'_' I can't read data from a disk using fs.read()

Is there any way I can read data from a disk?
faubiguy #6
Posted 19 October 2012 - 03:20 PM
To read data from file "filedata" on a disk below the computer do:
local filename = fs.combine(disk.getMountPath("bottom"), "filedata")
local file = fs.open(filename, "r")
local filedata = file.readAll()
file.close()

This will fail if the file doesn't exist.