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

How to read from a file using the fs API?

Started by trajing, 28 November 2014 - 10:09 PM
trajing #1
Posted 28 November 2014 - 11:09 PM
Asking because I'm having difficulty with this and I really want a method other than loading the file as an API.
johnnic #2
Posted 28 November 2014 - 11:17 PM

fileHandle=fs.open("folder/file.txt","r") --"r" as the second argument opens the file in read mode
text=fileHandle.readAll()
fileHandle.close()

The file will be stored in the variable text.
Exerro #3
Posted 28 November 2014 - 11:32 PM
Watch out for if the file doesn't exist. If it doesn't exist, you'll get an "attempt to call nil" error where you are calling fileHandle.readAll().
trajing #4
Posted 28 November 2014 - 11:39 PM
Thank you.