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

Converting the fs.open() read into a string

Started by CaosTECH, 31 October 2015 - 02:21 PM
CaosTECH #1
Posted 31 October 2015 - 03:21 PM
Hey I am wondering on how to convert the fs.open() read into a string, here is my example of code but it doesn't work.


if not fs.exists("file") then
file = fs.open("file", "w")
file.close()
end
write(":> ")
tx = read()
file = fs.open("file", "w")
file.write(tx)
file.close()
term.clear()
term.setCursorPos(1,1)
rFile = fs.open("file", "r")
txt = rFile.readAll()
print(txt)


But it does not print the text, I am creating a user custom username and password system using fs.open() and I can not get it to read the file and check if the user input = the files text
H4X0RZ #2
Posted 31 October 2015 - 03:40 PM
I tested your code and it works for me.

What CC version are you using? (this most likely isn't the problem)

Have you modified the FS API in any way?

Could you try out this code?

local file,content
if not fs.exists("file") then
  file = fs.open("file","w")
  write("New Name>")
  file.write(read())
  file.close()
end
file = fs.open("file","r")
content = file.readAll()
file.close()
write("Username>")
if(read() == content) then print("YAY") else print("NOPE") end
Edited on 31 October 2015 - 02:41 PM
KingofGamesYami #3
Posted 31 October 2015 - 05:32 PM
That snippet would not work if ran more than once, because you do not close the file after reading the contents. Always close files as soon as possible!!!