Jump to content




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


  • You cannot reply to this topic
10 replies to this topic

#1 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 07 October 2012 - 08: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)

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 07 October 2012 - 08: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)


#3 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 07 October 2012 - 08:59 PM

 Lyqyd, on 07 October 2012 - 08:50 PM, said:

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?

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 07 October 2012 - 09:03 PM

 sidekick_, on 07 October 2012 - 08:59 PM, said:

 Lyqyd, on 07 October 2012 - 08:50 PM, said:

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)


#5 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 07 October 2012 - 09:15 PM

The file should not be in programs folder, but in the computersfolder. --on ipod takes ages to fully explain

#6 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 07 October 2012 - 09: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.

#7 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 07 October 2012 - 09:16 PM

Cant Edith here:: ie the computers id folder ie 5 or45

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 07 October 2012 - 09:21 PM

 sidekick_, on 07 October 2012 - 09:03 PM, said:

 sidekick_, on 07 October 2012 - 08:59 PM, said:

 Lyqyd, on 07 October 2012 - 08:50 PM, said:

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.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 07 October 2012 - 09: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)


#10 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 07 October 2012 - 09: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)


#11 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 07 October 2012 - 10:03 PM

Thank you so much :D/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users