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

[Lua][Question] fs question

Started by Spongy141, 10 March 2013 - 07:43 PM
Spongy141 #1
Posted 10 March 2013 - 08:43 PM
I was wondering how I could read user information from on program from an other file I tried

file = fs.open("info","r")
file.readLine(info)
print(info)
file.close()
What am I doing wrong? And yes the other file does have info, and it looks like this.

local info = ("Hello!")
Please help.
superaxander #2
Posted 10 March 2013 - 08:50 PM
It is

file = fs.open("info","r")
info = file:readLine()
print(info)
file.close()
And your file must just be:

Hello!
Spongy141 #3
Posted 10 March 2013 - 08:55 PM
It is

file = fs.open("info","r")
info = file:readLine()
print(info)
file.close()
And your file must just be:

Hello!
Wow, I feel like a complete idiot. Thanks for the help though.
theoriginalbit #4
Posted 10 March 2013 - 08:58 PM
What am I doing wrong? And yes the other file does have info, and it looks like this.

local info = ("Hello!")
Please help.
If you wish to keep it like that you can do this


os.loadAPI('info')
print(info.info)
where the first info is the file name and the second info is the variable name.
immibis #5
Posted 10 March 2013 - 09:48 PM
It's file.readLine not file:readLine.
Either will work in this case, but the second one is still wrong.
Engineer #6
Posted 11 March 2013 - 07:52 AM
It's file.readLine not file:readLine.
Either will work in this case, but the second one is still wrong.

file:readLine() is for the IO API right?
LBPHacker #7
Posted 11 March 2013 - 08:01 AM

os.loadAPI('info')
print(info.info)
where the first info is the file name and the second info is the variable name.

In that case info mustn't be local…
Cyclonit #8
Posted 11 March 2013 - 09:35 AM
file:readLine() is part of the object file.
file.readLine() is part of the function table file.

Both work.
Spongy141 #9
Posted 11 March 2013 - 11:34 AM
So how will I get it to read a certain string by having it be a local in the file that's being read.
MysticT #10
Posted 11 March 2013 - 11:38 AM
file:readLine() is part of the object file.
file.readLine() is part of the function table file.

Both work.
Yes, but the table returned by fs.open is not ment to be used with : like the with io.open. Both should work with this function since it takes no arguments, but using : insted of . passes the table itself as the first argument, so using it with functions that take arguments (like file.write) wouldn't work.

So how will I get it to read a certain string by having it be a local in the file that's being read.
Do you want to read variables from another program? Or just information stored as text?
It's not possible (or at least too hard) to read a local variable from another program without modifying it, or interpreting it (unless of course it is made to return the value, in which case you just have to run the program and get the return value).
immibis #11
Posted 11 March 2013 - 12:25 PM
file:readLine() is part of the object file.
file.readLine() is part of the function table file.

Both work.
What's the difference between an "object" and a "function table"?

file:readLine() is just shorthand for file.readLine(file). Since file.readLine doesn't use any parameters, it's ignored and acts the same way as file.readLine() - that's why it still works.