474 posts
Posted 04 April 2012 - 10:04 PM
Fixed code:
h = fs.open("settings.cfg", "r")
text = h.readAll()
h.close()
print(text)
Should work.
4 posts
Posted 04 April 2012 - 11:55 PM
I apologize for postiong in the incorrect forum before.
I am trying to read text from a file in CraftOS.
Here is my code:
h = fs.open("settings.cfg", "r")
text = h.readLine()
h.close()
print(text)
I get an attempt to index ? error. What do I do?
Thanks.
454 posts
Location
London
Posted 05 April 2012 - 01:09 AM
I've moved thesbro's post here, but it seems that the time it was posted isn't affected, so yeah :V.
You should also put your code in a code tag, as it's easier to read.
4 posts
Posted 05 April 2012 - 09:31 PM
Fixed code:
h = fs.open("settings.cfg", "r")
text = h.readAll()
h.close()
print(text)
Should work.
This didn't work. I get the same error.
1604 posts
Posted 06 April 2012 - 01:08 AM
does the file exist?
because if it doesn't fs.open returns nil, hence the attempt to index a
nil value.
You should add a check to see if the file is open (it's not nil):
local file = fs.open("path/to/file", "r")
if file then
local text = file.readAll()
file.close()
print(text)
else
print("File doesn't exist.")
end
4 posts
Posted 06 April 2012 - 02:50 PM
does the file exist?
because if it doesn't fs.open returns nil, hence the attempt to index a
nil value.
You should add a check to see if the file is open (it's not nil):
local file = fs.open("path/to/file", "r")
if file then
local text = file.readAll()
file.close()
print(text)
else
print("File doesn't exist.")
end
Apparently it thinks that settings.cfg doesn't exist.
Does the path to file have to be absolute or relative?
settings.cfg is currently located in the disk folder and so is the program.
473 posts
Location
Poland
Posted 06 April 2012 - 03:05 PM
it has to be absolute, but you can try shell.resolve("relativepath")
6 posts
Posted 20 June 2012 - 04:47 AM
I apologize for postiong in the incorrect forum before.
I am trying to read text from a file in CraftOS.
Here is my code:
h = fs.open("settings.cfg", "r")
text = h.readLine()
h.close()
print(text)
I get an attempt to index ? error. What do I do?
Thanks.
try using
h = fs.open("settings.cfg", "a")
text = h.readAll()
h.close()
print(text)
fs.open("file", "-")
replace the "-" with:
"W" - To write
"R" - To read
"A" - Both read and write
but when you change to this code. note that it will create the file but it will give you nil
8543 posts
Posted 20 June 2012 - 07:37 AM
The modes are Read, Write, Append. Read is self-explanatory, Write overwrites the entire file when you write lines to it, Append adds to the end when you write lines to it. No mode allows both reading and writing.
6 posts
Posted 21 June 2012 - 02:15 AM
The modes are Read, Write, Append. Read is self-explanatory, Write overwrites the entire file when you write lines to it, Append adds to the end when you write lines to it. No mode allows both reading and writing.
Then I stand corrected. but that's just what i thought.
8543 posts
Posted 21 June 2012 - 03:26 PM
The modes are Read, Write, Append. Read is self-explanatory, Write overwrites the entire file when you write lines to it, Append adds to the end when you write lines to it. No mode allows both reading and writing.
Then I stand corrected. but that's just what i thought.
When providing help, it's best to not just guess or to state that one is merely guessing if one does. Misinformation presented as facts makes it that much more difficult to learn.