This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
How do i interact with files in a program?
Started by oxygencraft, 17 November 2015 - 04:04 AMPosted 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.
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.
Posted 17 November 2015 - 12:28 PM
to read/write text from/to a file you have to get a file handle:
Then depending on what you want to do you use different functions:
Take not that when you open a file using "w", you cannot read from it.
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
Posted 18 November 2015 - 11:21 PM
Don't forget fs.close - always close your files when you're done with them.
Posted 19 November 2015 - 02:09 AM
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
Posted 19 November 2015 - 02:36 AM
Yeah, I stated that rather poorly - thanks for correcting and clarifying that :)/>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.