645 posts
Location
'Merica
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.
620 posts
Location
Holland
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!
645 posts
Location
'Merica
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.
7508 posts
Location
Australia
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.
997 posts
Location
Wellington, New Zealand
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.
1522 posts
Location
The Netherlands
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?
758 posts
Location
Budapest, Hungary
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…
50 posts
Location
Germany
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.
645 posts
Location
'Merica
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.
1604 posts
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).
997 posts
Location
Wellington, New Zealand
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.