Advertisement
Rolcam

Computercraft Tutorial - Section 9 - File System

Jun 14th, 2021 (edited)
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. --This is part of a Computercraft Programming Teaching Program
  2.  
  3. tSides = {"left","right","bottom","top","front","back"}
  4.  
  5. for i = 1, #tSides do
  6.   monitor = peripheral.wrap(tSides[i])
  7.   if monitor then
  8.         side = tSides[i]
  9.         break
  10.   end
  11. end
  12. --Prevents program termination
  13. os.pullEvent = os.pullEventRaw
  14. term.redirect(peripheral.wrap(side))
  15.  
  16. -- Resets Screen
  17. function reset()
  18. term.setTextColor(colors.white)
  19. term.setBackgroundColor(colors.black)
  20. term.clear()
  21. term.setCursorPos(1,1)
  22. end
  23.    
  24. reset()
  25. print("Section 9 - File System \n ")
  26. print("These commands are used to edit/create/delete files on the computer from within the program.")
  27. print("Some uses for these can be record keeping, program config files, making a database, etc.")
  28. print(" ")
  29. print("file1 = fs.open(\"file\", \"a\") - uses \"file1\" as a handle for the file \"file\"")
  30. print("Notice the \"a\" at the end. That set the file mode to append. That means writing to the file will add onto it.")
  31. print("Other file modes are \"r\" - Read Only, and \"w\" - write. Write mode overwrites a file with new data.")
  32. print("fs.exists(\"<file path>\") - Returns a boolean value if the file exists or not. Can be used as a condition for loops and statements")
  33. print("if fs.exists(\"file\") == true then <--- Example Usage")
  34. print("file1.write(\"test\") - Writes \"test\" to the file \"file\"")
  35. print("file1.writeLine(\"test\") - Same as fs.write(), except it makes a new line after")
  36. print("Think of fs.write() as write() and fs.writeLine() as print()")
  37. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement