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

Help with fs API

Started by Cranium, 30 October 2012 - 01:08 PM
Cranium #1
Posted 30 October 2012 - 02:08 PM
I seem to be hitting a snag when it comes to creating a user file. When calling this function in my code, it says "attempt to index ? (a nil value)". This occurs when i am trying to call back to the fs api through userFile.writeLine() I have tried removing the table and textutils call, but I still get the error. I have no idea why though. I have wrapped the fs api as I thought I am supposed to, as documented in the wiki. I can call back to the fs api properly in the lua prompt. Why can't I save anything within this program though? Here is the function, but If you need the full code, here is the pastebin link: http://pastebin.com/GWUUfNeh



local function checkFiles()
  while true do
    if not fs.exists(".smartOS/userList") then
      local tUsers = {}
      tUsers[1] = {}
      tUsers[1].username = "SmartOSAdmin"
      tUsers[1].password = "SmartOSAdminPassword"
      tUsers[1].tZ = "utc"
      tUsers[1].level = 1
      local userFile = fs.open(".smartOS/userList","w")
      userFile.writeLine(textutils.serialize(tUsers))
      file.close()
      userExists = false
    elseif not fs.exists(".smartOS/smartAPI") then
      local updateSite = http.get("http://pastebin.com/raw.php?i=code")
      local siteFile = updateSite.readAll()
      local writeFile = fs.open(".smartOS/smartAPI","w")
      writeFile.write(siteFile)
      writeFile.close()
    else
      break
    end
  end
end
KaoS #2
Posted 30 October 2012 - 02:10 PM
on line 12 "file.close()" should be "userFile.close()" that will sort it out as the values are only written once you close the handle
Cranium #3
Posted 30 October 2012 - 02:12 PM
Nope….. I fixed that, but still get the same error. Line 55 of the full program if you need to know.
KaoS #4
Posted 30 October 2012 - 02:19 PM
try using the io api, I know that works. converted code:


local function checkFiles()
  local userExists = true
  while true do
   if not fs.exists(".smartOS/userList") then
    local tUsers = {}
    tUsers[1] = {}
    tUsers[1].username = "SmartOSAdmin"
    tUsers[1].password = "SmartOSAdminPassword"
    tUsers[1].tZ = "utc"
    tUsers[1].level = 1
    local userFile = io.open(".smartOS/userList","w")
    userFile:write(textutils.serialize(tUsers))
    userFile:close()
    userExists = false
   elseif not fs.exists(".smartOS/smartAPI") then
    local updateSite = http.get("http://pastebin.com/raw.php?i=code")
    local siteFile = updateSite.readAll()
    local writeFile = io.open(".smartOS/smartAPI","w")
    writeFile:write(siteFile)
    writeFile:close()
   else
    return true,userExists
   end
  end
end
Cranium #5
Posted 30 October 2012 - 02:25 PM
………nope……….
Why is this not working?????
I know by looking at the code that this should work, but it isn't.
KaoS #6
Posted 30 October 2012 - 02:27 PM
did you restart the PC before trying it, if you do not close a file handle then often it will not allow you to write to that file name because it is in use. are you sure the path is valid though… that could be the problem
Cranium #7
Posted 30 October 2012 - 02:33 PM
Yeah, the ".smartOS" directory did not exist. Updated it to add that if it did not exist, and the error went away. Getting new errors, but I think i can fix them. Thanks for the help though!
Cranium #8
Posted 30 October 2012 - 02:34 PM
One quick question though, does an API have to be in the root directory or the API folder to be loaded correctly?
KaoS #9
Posted 30 October 2012 - 02:35 PM
excellent :P/>/>

they must be there to load automatically. just call os.loadAPI to load an api anywhere