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

Someone familiar with IO?

Started by Noodle, 06 August 2012 - 07:46 AM
Noodle #1
Posted 06 August 2012 - 09:46 AM
Anyone familiar with the IO library help me?
I'm trying to open a file in my appdata (not inside minecraft - lua compiler)
So far this code:
hFile = io.open("options.ini", "r")
nickname = hFile:read("*a") -- error line
hFile:close()
I know it doesn't work. Anyone who is familiar with this can help, please?
EDIT: Error is 'attempt to index global file'
Noodle #2
Posted 06 August 2012 - 10:17 AM
Fixed code a bit…
Still doesn't work.
Theepot #3
Posted 06 August 2012 - 10:47 AM
AFAIK computercraft won't let you access files outside of your own computercraft computer/turtle.
Noodle #4
Posted 06 August 2012 - 10:50 AM
I said its a lua compiler..
Theepot #5
Posted 06 August 2012 - 10:56 AM
Oh, my bad. Does options.ini exist in the environment of your script? On Windows you could try os.execute("dir") to see in what directory you are. on Linux you could do os.execute("pwd").
Noodle #6
Posted 06 August 2012 - 10:58 AM
Yes, It exists.
Theepot #7
Posted 06 August 2012 - 11:06 AM
And your script is running in the same directory as options.ini? Because it seems to me that hFile is nil. And io.open returns nil when it can't find or open the file. Also, if you try this:

local file, msg = io.open("options.ini", "r")
if not file then
   print(msg)
end
You can view io.open's error message.
Noodle #8
Posted 06 August 2012 - 11:11 AM
Returns the same error
Its line 6 so its on the nickname = read line.
Theepot #9
Posted 06 August 2012 - 11:21 AM
So let me get this straight:
hFile isn't nil, if you do os.execute("dir") in your script, options.ini is actually listed, yet read still doesn't work?
The only thing I can still think of is that option.ini is in some way protected by your OS, but that seems unlikely to me.

EDIT:
If you have options.ini open in another program, say notepad, it might also not work.
Noodle #10
Posted 06 August 2012 - 11:28 AM
Its not open when I'm using it -.-
os.execute("dir") doesn't work.
Theepot #11
Posted 06 August 2012 - 11:40 AM
What do you mean doesn't work, can it not find os.execute? To clarify: You are just running your script through Lua in a console i.e.: not Lua embedded in another program?
If it can't find os.execute you might have set your function environment to somewhere it can't find the os functions (with setfenv maybe?). Or you are running an OS that miraculously doesn't have dir.
Noodle #12
Posted 06 August 2012 - 11:56 AM
Lol
I'm compiling it in lua thru a console. Everything should work, but it isn't.
Pharap #13
Posted 06 August 2012 - 11:57 AM
Anyone familiar with the IO library help me?
I'm trying to open a file in my appdata (not inside minecraft - lua compiler)
So far this code:
hFile = io.open("options.ini", "r")
nickname = hFile:read("*a") -- error line
hFile:close()
I know it doesn't work. Anyone who is familiar with this can help, please?
EDIT: Error is 'attempt to index global file'
have you tried using periods instead of colons? eg hFile.read("*a") and hFile.close() instead of hFile:read("*a") and hFile:close()?
Noodle #14
Posted 06 August 2012 - 11:59 AM
FILE.close() is in CC not in actual Lua.
Actual lua is
io.read(blah)
or
FILE:read("Stringpattern")
Noodle #15
Posted 06 August 2012 - 03:00 PM
NVM
My brother just told me that he recoded the io.open thing fully. It now uses something he is familiar with and I'll have to spend time learning - ,-