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

[LUA][Help]readLine() just isn't working for me :X

Started by remiX, 07 October 2012 - 07:03 PM
remiX #1
Posted 07 October 2012 - 09:03 PM
Hey really need some help here. readLine() just isnt working for me even within this simple code:

file = fs.open("derp","r")
line = file.readLine() -- tried local line = file.readLine(), also tried readAll()
file.close()
print(line)

Whats wrong? :X

Edit: forgot error: save:2: attempt to index ? (a nil value)
Lyqyd #2
Posted 07 October 2012 - 09:50 PM
Make sure the file is being opened successfully:


file = fs.open("blah", "r")
if file then
    line = file.readLine()
    file.close()
end
print(line)
remiX #3
Posted 07 October 2012 - 09:59 PM
Make sure the file is being opened successfully:


file = fs.open("blah", "r")
if file then
	line = file.readLine()
	file.close()
end
print(line)

Ahh damn. Thank you so much :D/>/> But now it prints a blank line. In my separate file that it reads i have something written on line 1. Why?
remiX #4
Posted 07 October 2012 - 10:03 PM
Make sure the file is being opened successfully:


file = fs.open("blah", "r")
if file then
	line = file.readLine()
	file.close()
end
print(line)

Ahh damn. Thank you so much :D/>/> But now it prints a blank line. In my separate file that it reads i have something written on line 1. Why?

edit: Clicked quote instead of edit, sorry xD
edit: I have this code but it isn't printing … Why isn't it finding the file? I have it (Tekkit_Server_3.1.2modsComputerCraftluaromprogramscomputer) for my multiplayer server.

file = fs.open("derp", "r")
if file then
	print("...")
	line = file.readLine()
	file.close()
end
print(line)
sjele #5
Posted 07 October 2012 - 10:15 PM
The file should not be in programs folder, but in the computersfolder. –on ipod takes ages to fully explain
brett122798 #6
Posted 07 October 2012 - 10:16 PM
I'm having a similar problem right now. I need to read the first line of a file, which is a variable and then print that variable onto the screen.
sjele #7
Posted 07 October 2012 - 10:16 PM
Cant Edith here:: ie the computers id folder ie 5 or45
Lyqyd #8
Posted 07 October 2012 - 10:21 PM
Make sure the file is being opened successfully:


file = fs.open("blah", "r")
if file then
	line = file.readLine()
	file.close()
end
print(line)

Ahh damn. Thank you so much :D/>/> But now it prints a blank line. In my separate file that it reads i have something written on line 1. Why?

edit: Clicked quote instead of edit, sorry xD
edit: I have this code but it isn't printing … Why isn't it finding the file? I have it (Tekkit_Server_3.1.2modsComputerCraftluaromprogramscomputer) for my multiplayer server.

file = fs.open("derp", "r")
if file then
	print("...")
	line = file.readLine()
	file.close()
end
print(line)

You would need to open the whole path. If it's in rom, the whole path would be something like /rom/programs/computer/file. Of course, you won't be able to write to it there (can't write to rom), but you should be able to read it fine with the correct path.
remiX #9
Posted 07 October 2012 - 10:34 PM
Sigh I just can't get this to work >_<

the exact path for the file that it reads is: H:HurrdurrGamesMCTekkit_Server_3.1.2modsComputerCraftluaromprogramscomputersavedvarderp – where derp is the file which is a .txt file and still doesn't work.

file = fs.open("H:HurrdurrGamesMCTekkit_Server_3.1.2modsComputerCraftluaromprogramscomputersavedvarderp", "r")
if file then
	    line = file.readLine()
	    file.close()
end
print(line)
Lyqyd #10
Posted 07 October 2012 - 10:45 PM
The full path, as seen by the computer, using forward slashes, not backslashes. And if it's a .txt file, you need to have the whole file name, including the .txt.

file = fs.open("/rom/programs/computer/savedvar/derp.txt", "r")
if file then
		line = file.readLine()
		file.close()
end
print(line)
remiX #11
Posted 07 October 2012 - 11:03 PM
Thank you so much :D/>/>