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

How do i interact with files in a program?

Started by oxygencraft, 17 November 2015 - 04:04 AM
oxygencraft #1
Posted 17 November 2015 - 05:04 AM
What i am trying to do is make an API and i want to make a username and password function so the user needs to enter a username and password to exit to the terminal or something else. Each user will be stored in a file and its the same with the password BUT the passwords is hashed, the passwords are stored in a different file.
KingofGamesYami #2
Posted 17 November 2015 - 05:34 AM
fs API
oxygencraft #3
Posted 17 November 2015 - 09:03 AM

Well i looked there before i started asking for help and i cant find a function to read/write text to files.
Dragon53535 #4
Posted 17 November 2015 - 09:49 AM
Tutorial
Creator #5
Posted 17 November 2015 - 12:28 PM
to read/write text from/to a file you have to get a file handle:


local handle = fs.open(path,"w") -- "w" is for write
local handle = fs.open(path,"r") -- "r" is for read
local handle = fs.open(path,"a") -- "a" is for append

Then depending on what you want to do you use different functions:


Read:
handle.readAll()
Write:
handle.write(data)
Append:
handle.write(data)

Take not that when you open a file using "w", you cannot read from it.
Edited on 17 November 2015 - 11:28 AM
Twijn #6
Posted 18 November 2015 - 10:25 PM
FS API, fs.open

That should be all you need for basic file manipulating. :)/>
Edited on 18 November 2015 - 09:25 PM
Dog #7
Posted 18 November 2015 - 11:21 PM
FS API, fs.open

That should be all you need for basic file manipulating. :)/>
Don't forget fs.close - always close your files when you're done with them.
Twijn #8
Posted 19 November 2015 - 02:09 AM
FS API, fs.open

That should be all you need for basic file manipulating. :)/>
Don't forget fs.close - always close your files when you're done with them.

h.close is not a part of the FS api, it is a part of what is returned from fs.open (which is a table, basically), and it's also listed in the fs.open link above, however it is important and worth noting that you need to close the files as well. :)/>

Also, if you accidentally forget to close it (which I have done before) then it's fairly simple to fix it. Simply restart the computer. I'm not positive on whether or not the changes you made to it are kept when you don't close it. If you forget to close it you will often quickly realize because you cannot delete the file or even rename it or open it again as far as I know.
Edited on 19 November 2015 - 01:15 AM
Dog #9
Posted 19 November 2015 - 02:36 AM
FS API, fs.open

That should be all you need for basic file manipulating. :)/>
Don't forget fs.close - always close your files when you're done with them.

h.close is not a part of the FS api, it is a part of what is returned from fs.open (which is a table, basically), and it's also listed in the fs.open link above, however it is important and worth noting that you need to close the files as well. :)/>

Also, if you accidentally forget to close it (which I have done before) then it's fairly simple to fix it. Simply restart the computer. I'm not positive on whether or not the changes you made to it are kept when you don't close it. If you forget to close it you will often quickly realize because you cannot delete the file or even rename it or open it again as far as I know.
Yeah, I stated that rather poorly - thanks for correcting and clarifying that :)/>