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

Help with FS API

Started by Phoenix323, 12 December 2014 - 10:12 PM
Phoenix323 #1
Posted 12 December 2014 - 11:12 PM
I don't really get the fs open
Cranium #2
Posted 13 December 2014 - 12:33 AM
I've split your question into its own topic, as the topic you replied to is fairly old.

What don't you get, do you have any code that you need help with? I'm sure if you provide additional info, others would be willing to help you understand.
ebernerd #3
Posted 13 December 2014 - 12:39 AM
I don't really get the fs open

Well, it depends on how you want to use it.
You need to declare a variable
I personally use "f" but you can use anything you want

local f = fs.open(<filename>,<mode>)

So, there are three modes: W (write), R (read), and A (append).
Write mode erases the contents of the file, and writes what you want; Read mode can only read from the file; Append mode writes to the file, but keeps the content of the file, and writes right to the end of the previous addition. YOU MUST CLOSE THE FILE WHEN DONE WITH IT. If you don't, you risk corrupting the file.

How to use it:

WRITE MODE

local f = fs.open("file","w")
f.write("Write this to the file!") --replace f with whatever you declared the variable as
f.close() --CRUCIAL. Make sure you do this when you're done with

READ MODE

local f = fs.open("file","r")
local content = f.readAll() --Declare another variable
f.close()
print(content) --Prints content of file

APPEND MODE:

local f = fs.open("file","a")
f.write("Write this right to the end of the file")
f.close()

Hope this helped!
Edited on 13 December 2014 - 12:43 AM
Lignum #4
Posted 13 December 2014 - 01:42 AM
YOU MUST CLOSE THE FILE WHEN DONE WITH IT. If you don't, you risk corrupting the file.
No, you don't. If you don't close it, other programs can't access it (more or less). That's all, omitting it won't corrupt the file. Even if you don't close the file yourself, the Lua GC will take care of it eventually. But it's still good practice to do so.
Also, please use local variables in your code. Globals can be accessed everywhere, even from other programs.
ebernerd #5
Posted 13 December 2014 - 01:43 AM
YOU MUST CLOSE THE FILE WHEN DONE WITH IT. If you don't, you risk corrupting the file.
No, you don't. If you don't close it, other programs can't access it (more or less). That's all, omitting it won't corrupt the file. Even if you don't close the file yourself, the Lua GC will take care of it eventually. But it's still good practice to do so.
Also, please use local variables in your code. Globals can be accessed everywhere, even from other programs.
Thanks for the update. I'll update the code.
Dragon53535 #6
Posted 13 December 2014 - 01:57 AM
No, you don't. If you don't close it, other programs can't access it (more or less).
If you don't close it, nothing gets saved into the file unless you flush it.
If you don't close it, you cannot delete the file until the computer is restarted.
Phoenix323 #7
Posted 13 December 2014 - 06:18 AM
Well Thank you i am starting to it understand more now