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

Help with reading files

Started by MarcoPolo0306, 29 December 2017 - 09:55 PM
MarcoPolo0306 #1
Posted 29 December 2017 - 10:55 PM
I am making a password-protected OS with encrypted password using the Crypt api, and I am having problems reading. I think the problem is it isn't reading properly, and it won't read it. If I try doing print(pass:readAll()) it returns nothing.
Heres my code:


os.loadAPI("M-OS_Rebirthed/crypt")
local w,h = term.getSize()
local pass = fs.open("M-OS_Rebirthed/config/pass.txt", "r")
term.setBackgroundColor(colors.gray)
term.clear()
local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end
local function writeCenterText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)
end
local function drawscreen()
term.setCursorPos(w/2+25/2,3)
local logo = paintutils.loadImage("M-OS_Rebirthed/img/logo.img")
paintutils.drawImage(logo,25/2,h/2-3)
term.setCursorPos(1,h/2+3)
term.setBackgroundColor(colors.gray)
centerText("Welcome to M-OS")
end
drawscreen()
if pass.readLine() == "" then
shell.run("M-OS_Rebirthed/desktop.lua")
else
while true do
  term.setCursorPos(1,h/2+4)
  centerText("Please your enter password:")
  term.setCursorPos(1,h/2+5)
  term.setBackgroundColor(colors.lightGray)
  writeCenterText("					  ")
  term.setCursorPos(math.ceil((w / 2) - (string.len("					  ") / 2)), h/2+5)
  print(pass.readAll())
  input = read("#")
  term.clear()
  term.setTextColor(colors.black)
  term.setCursorPos(1,1)
  print(crypt.encrypt(input))
  print(pass.readAll())
 
  sleep(10)
  if crypt.encrypt(input) == pass.readAll() then
   term.setBackgroundColor(colors.gray)
   term.clear()
   drawscreen()
   term.setCursorPos(1,h/2+3)
   term.clearLine()
   term.setBackgroundColor(colors.gray)
   centerText("Welcome!")
   sleep(2)
   shell.run("M-OS_Rebirthed/desktop.lua")
  else
   drawscreen()
   term.setCursorPos(1,h/2+3)
   term.clearLine()
   term.setBackgroundColor(colors.gray)
   centerText("Password incorrect, please try again.")
  end
end
end

Thanks!
valithor #2
Posted 29 December 2017 - 11:03 PM
This is due to how readAll actually works. Whenever you open a file for reading you will only ever read each part of it once no matter what functions you call to do it. So, if your file only contains a single line and you read that single line with readLine as you are doing in your if statement, then there is no function you can call to reread that line without closing and reopening the file.

The best way to solve your problem is to save the result of the readLine into a variable and then using that variable anywhere you need to reference the password.


local password = pass.readLine()
if password == "" then

edit:

also remember to close any file you open after you are finished using them
Edited on 29 December 2017 - 10:05 PM
MarcoPolo0306 #3
Posted 29 December 2017 - 11:20 PM
Thanks! Worked perfectly.