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

(Help) File readLine()

Started by Hayden_Almeida, 22 January 2014 - 03:10 PM
Hayden_Almeida #1
Posted 22 January 2014 - 04:10 PM
Hello all, iam new here and i need help. I think its simple, lets begin:

I have one program, called "usuarios" and i have there:


Hayden
666

now, i Have on the Startup program:

usuario = read()
senha = read()
h = fs.open("usuarios", "r")
u = h.readLine()  -- for Read "Hayden"
s = h.readLine() -- for Read "666"
h.close()
if usuario == u then
print("ok.")
else
print("Not work.")
sleep(2)
os.reboot()
end

When i Type "Hayden" its says is not the same… so i ask, why?
If usuario == u i think the logic is the what i type ("hayden") is the same what the file reads "Hayden" and its returns :


print("Not work.")
Lyqyd #2
Posted 22 January 2014 - 05:04 PM
Capitalization is important. "Hayden" and "hayden" are not the same.
Hayden_Almeida #3
Posted 22 January 2014 - 05:44 PM
Capitalization is important. "Hayden" and "hayden" are not the same.

I know man.
Lyqyd #4
Posted 22 January 2014 - 07:45 PM
Try printing the value of u and the value of usuario.
surferpup #5
Posted 22 January 2014 - 09:03 PM
Since I hand't messed around with files yet in ComputerCraft, I thought I would duplicate your scenario and see what was happening. I simplified it down a bit and wrote the following code:


local fileName = "myFile"
local line
local userInput = read()

if fs.exists(fileName) then
  local hRead = fs.open(fileName,"r")
  line = hRead.readLine()
  hRead.close()
else
  print ("File "..fileName.." does not exist.  Program exiting…")
  return
end

if line==userInput then
  print("User Input and File Content matches."
else
  print("User Input and File Content Error:"
  print("User Input  -->"..userIntput.."<--")
  print("File Content-->"..line.."<--")
end

I am placing a minimal amount of error checking in my code. I print a message and quit the program if the file does not exist. I also will print what the user typed and what the file contained if there is a mismatch.

I then created a file named myFile and edited it to contain the following line:

surferpup wuz here

I ran the code and it worked perfectly. If I messed up the file name it printed a message and quit. If I typed in what was read from the file it worked. If I typed something different it worked. I tried a file with only one line (no end-of-line). I tried multiple line files. I tried data with and without capital letters. I also tried it with your values – I used "Hayden" with quotes and without for both the file data as well as the user input. Worked fine. Everything worked exactly as expected. Please check your code. Or, copy mine and test it yourself.
Edited on 22 January 2014 - 08:18 PM