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

File level

Started by thomas.h, 21 July 2012 - 10:44 AM
thomas.h #1
Posted 21 July 2012 - 12:44 PM
Is it possible to go up a level when using fs.open

I tried this


users = fs.open("../users", "r")

but it gave me the error


addid:18: attempt to index ? (a nil value)

Thanks for any help
Noodle #2
Posted 21 July 2012 - 12:48 PM
You are opening a directory
You need to open the files

userTbl = fs.list("../users")
for i = 1,#userTbl do
hFile = fs.open(userTbl, "r")
readFile = hFile.readAll()
hFile.close()
end

EDIT: IDK if this works (untested) but it should..
KevinW1998 #3
Posted 21 July 2012 - 12:49 PM
If I aren't wrong then fs.open is using the absolute path and not the local path of the program!

users = fs.open("users","r")
Edit: Sorry I've missed the question :)/>/>
Noodle #4
Posted 21 July 2012 - 12:51 PM
users.close() – That should be ur error if you aren't closing it.
Why do you need to open it?
thomas.h #5
Posted 21 July 2012 - 01:16 PM
Here is my whole code at the moment


while true do
	shell.run("clear")
	print("Computer ID :".. os.computerID())
	print("TT Industries")
	print("Add to authentication list")
	print("-------------------------------------------------")
	print("")
  
	print("Computer ID to add: ")
  
	text = read()
  
	while true do
		users = fs.open("../users", "r")
		while true do
			line = users.readLine()
			if not line then
				users.close()
				print("User not found. Authenticating...")
				users = fs.open("../users", "a")
				users.writeLine(text)
				users.close()
				print("User added to list")
				break
			else
				if line == text then
					users.close()
					print("ID already authenticated.")
					break
				end
			end
		end
		break
	end
  
end

It used to work with just "users" but when I moved the file back a level and tried "../users" that did not work
Noodle #6
Posted 21 July 2012 - 01:20 PM
users = fs.open("../users", "a") - Why not just "w"
You are still opening a directory, what is your main goal?
thomas.h #7
Posted 21 July 2012 - 01:30 PM
My goal is to append the user ID the user file that's what I thought "a" was for.
http://computercraft...p?title=Fs.open

Also the users file does not have an extension as I created it using the computer on my world
Noodle #8
Posted 21 July 2012 - 01:43 PM
The ID..
You should fs.open(text, "r")
thomas.h #9
Posted 21 July 2012 - 02:19 PM
I don't think you understand me.

This is the process

1) User inputs id to add to list
2) Computer opens users and adds whatever the user in-putted onto the list


fs.open(text, "a")
Would open a file with a name of the user ID and not the user list file which is "users" (Doesn't have an extension)
Noodle #10
Posted 21 July 2012 - 03:41 PM
I see, You are writing a new line for that ID.
do fs.open("names", "a") – ../names is a dir
Lyqyd #11
Posted 21 July 2012 - 09:15 PM
What are you trying to do with this whole "move back a level" thing? Are you trying to read/write to files inside the computer's file system, or outside of it? If you're trying to go to the folder that contains to root of the computer's filesystem in your actual filesystem (that is, trying to access the /computer/ directory inside your world folder), you can't. Also, the fs API needs absolute paths, and the .. mechanism is relative pathing.