This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
moving a file with a input into a directory
Started by AnDwHaT5, 18 March 2013 - 10:22 AMPosted 18 March 2013 - 11:22 AM
Its in the name i need to move a file with a input in one program to a directory look at near bottom comment.
Posted 18 March 2013 - 11:26 AM
local handle = io.open(directory/file,"r") --Opens the file in the directory
local data = handle:read() --Reads all the data on the first line of the file
handle:close() --Closes the handle
input = read() --Get user password/username
if input ~= data then --Compare the input with the contents of the file
--Wrong Password
else
--Right Password
end
You can open files in directories by defining the directory first then seperate the file with a "/" like so:
users/AnDwHaT5
Or if you have directories in directories:
data/users/AnDwHaT5/username/password
Posted 18 March 2013 - 11:28 AM
yes but what if the file is a input
Posted 18 March 2013 - 11:32 AM
yes but what if the file is a input
Oh, I see:
local tab = fs.list("users")
local correct = false
repeat
input = read()
for i = 1,#tab do
if input ~= tab[i] then
correct = false
else
correct = true
end
end
until correct
--If the user makes it past this point, the username is correct
Edited on 18 March 2013 - 10:47 AM
Posted 18 March 2013 - 11:44 AM
ok we have three things we are working with users AnDwHaT5 and lets say hello. So in hello we want to get into users which is a directory and get into AnDwHaT5. So lets say in AnDwHaT5 it says hi. The input for the password in hello is no. I want it to look in users/AnDwHaT5 and see if it is right or not. Remember the imput can be anything dont take so literally when i say no is the input its ..input.
Posted 18 March 2013 - 11:48 AM
EDIT: Is this what you mean?
File Writing Data to Hello in users/AnDwHaT5/hello
input = read()
local handle = io.open("users/AnDwHaT5/hello","w")
handle:write(input)
handle:close()
File reading the data and comparing it to users/AnDwHaT5
local handle = io.open("users/AnDwHaT5,"r")
local data = handle:read()
handle:close()
local handle = io.open("users/AnDwHaT5,"r")
local readData = handle:read()
handle:close()
if data ~= realData then
--Incorrect
else
--Correct
end
Posted 18 March 2013 - 11:57 AM
Ok i got that i need help with this code. i need it to read the pass in the file.
repeat
term.clear()
term.setCursorPos(1,1)
write("User: ")
user = read()
write("Pass: ")
pass = read()
local tab = fs.list("users")
local correct = false
for i = 1,#tab do
if user ~= tab[i] then
correct = false
else
correct = true
end
end
until correct
local content = {}
local line = f.readLine()
while line do
table.insert(content, line)
line = f.readLine()
end
if content[1] == pass then
print("hi")
else
print("nope")
end
It dosnt want to work.Posted 18 March 2013 - 12:03 PM
What file are you trying to open with f.readLine()?
Posted 18 March 2013 - 12:07 PM
Honestly that was part of a old code here is the better one
repeat
write("User: ")
user = read()
write("Pass: ")
pass = read()
local tab = fs.list("users")
local correct = false
for i = 1,#tab do
if user ~= tab[i] then
correct = false
else
correct = true
end
end
until correct
local content = {}
end
All i need is to read the pass from the File in the directoryPosted 18 March 2013 - 12:13 PM
How many lines are you reading from? One or more?
Posted 18 March 2013 - 12:15 PM
1
wasnt that the longest post in the forums ^
the one that is it wont stop fusing the edits
wasnt that the longest post in the forums ^
the one that is it wont stop fusing the edits
Posted 18 March 2013 - 12:17 PM
Well gladly I think it is over ^_^/>
repeat
write("User: ")
user = read()
write("Pass: ")
pass = read()
local tab = fs.list("users")
local correct = false
for i = 1,#tab do
if user ~= tab[i] then
correct = false
else
correct = true
end
end
until correct
local fileLocation = ("users/"..user)
local handle = io.open(fileLocation,"r")
local data = handle:read()
handle:close()
if pass ~= data then
print("WRONG!")
else
print("CORRECT!")
end
Posted 18 March 2013 - 12:26 PM
Thank you so much!
Posted 18 March 2013 - 12:29 PM
It is my pleasure! ^_^/>Thank you so much!
Posted 18 March 2013 - 12:32 PM
BTW one last thing so i dont have to post again uhh how do i move a file using a input to a directory i mean i know how without a input. I would think something like fs.move(user, "/users") but it dosnt work
Posted 18 March 2013 - 12:37 PM
I'm not sure if I understand but:BTW one last thing so i dont have to post again uhh how do i move a file using a input to a directory i mean i know how without a input. I would think something like fs.move(user, "/users") but it dosnt work
fs.move(user,"users/")
A backslash in the front is simply a coder's choice/preference. I for one, like using it.a/b/c is the same as /a/b/c
A backslash at the end, however, indicates a directory
Posted 18 March 2013 - 12:40 PM
file exists it says and no it dosnt in the directory
Posted 18 March 2013 - 12:44 PM
Whoops my bad:
local location = ("users/"..user)
fs.move(user,location)
Posted 18 March 2013 - 01:10 PM
Thats what worked! Thanks i can now finish my user and pass system!
Posted 18 March 2013 - 01:16 PM
Woot! ^_^/>Thats what worked! Thanks i can now finish my user and pass system!
Posted 18 March 2013 - 05:46 PM
for i = 1,#tab do
if user ~= tab[i] then
correct = false
else
correct = true
end
end
??if fs.exists(user) then
-- file exists :??
end