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

Reading text from file

Started by cant_delete_account, 04 April 2012 - 08:04 PM
cant_delete_account #1
Posted 04 April 2012 - 10:04 PM
Fixed code:

h = fs.open("settings.cfg", "r")
text = h.readAll()
h.close()
print(text)
Should work.
leo.chadvader #2
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.
Advert #3
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.
leo.chadvader #4
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.
MysticT #5
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
leo.chadvader #6
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.
Liraal #7
Posted 06 April 2012 - 03:05 PM
it has to be absolute, but you can try shell.resolve("relativepath")
Mitzey234 #8
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
Lyqyd #9
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.
Mitzey234 #10
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.
Lyqyd #11
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.