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

fs.open troubles.

Started by leftcatcher, 15 October 2012 - 05:43 PM
leftcatcher #1
Posted 15 October 2012 - 07:43 PM
Hello, I'm having a little bit of trouble with a program of mine. It says that I have a nil value at line 11 (marked line). I can't quite figure out the problem with it, as it works when I have the path (in line 10) at "disk/install", but not "disk/install/key". Any help would be appreciated, thank you.


  key = math.random(1000,9999).."-"..math.random(1000,9999)
    keyfile = fs.open("disk/install/key", "w")
keyfile.write(pdkey) --This line, but I believe it has to do with the line above it.
    keyfile.close()
installPrograms()
Ditto8353 #2
Posted 15 October 2012 - 07:47 PM
Sounds like the file doesn't exist in directory disk/installer/key
sjele #3
Posted 15 October 2012 - 07:51 PM
Whole code? pdkey is nil, or the files is not there
leftcatcher #4
Posted 15 October 2012 - 08:09 PM
Well, it was my belief that fs.open would also make the file, as it seems to in another program I use, which this is essentially copied from. Then again, it's only two long as well. I bet it's then that the file isn't there, as you said. How would I go about creating it so I could make it open it for writing?

I realized I don't need to do this for this program, but it would be great for future use, should I ever need it.

Also, I realized that I had pdkey instead of key for the variable, got into the habit of using that since I was working on something else with the pdkey variable right before. xD I changed that to just "key" and it still marks it as nil. Change listed below:


  key = math.random(1000,9999).."-"..math.random(1000,9999)
    keyfile = fs.open("disk/install/key", "w")
keyfile.write(key) --This line, but I believe it has to do with the line above it.
    keyfile.close()
installPrograms()
Mads #5
Posted 15 October 2012 - 08:22 PM
Use this function:

function exists(filename)
    return (io.open(filename) == nil and false or true) -- can be shortened to (io.open(filename) == nil)
end
Ditto8353 #6
Posted 15 October 2012 - 08:29 PM
Use this function:

function exists(filename)
	return (io.open(filename) == nil and false or true) -- can be shortened to (io.open(filename) == nil)
end

io.open(fileName) does the same thing and more than this. io.open() returns nil, <error> when unsuccessful. No need to add more overhead.
leftcatcher #7
Posted 15 October 2012 - 08:30 PM
So basically, that would allow me to use exists("disk/installdisk/key") and it would create it for me and get rid of the nil error? What confuses me is that it's basically what I have in my code, just in a separate function.
Lyqyd #8
Posted 15 October 2012 - 11:45 PM
Opening a file handle for writing will create the file, but not any directories. Ensure the directory you're trying to write the file to exists first.
leftcatcher #9
Posted 16 October 2012 - 12:35 AM
Well, it's writing to the disk, so I would assume the disk directory exists. ._. Which is why it confuses me. Unless the 'installdisk' after 'disk' is actually the directory, in which case that doesn't exist. I'll try that I guess.