Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

[ io ] io:open("w"), Permission Denied
Started by PlayZot_Christopher Dec 06 2018 01:08 PM

2 replies to this topic
json write

Best Answer PlayZot_Christopher , 06 December 2018 - 02:25 PM

After further testing, I was able to make it work.

 

The source of the problem was that in at the top of the 'build' function, in the function jcodec["exist"], the file gets opened for reading but I forgot to close the file after it was done and nil it.

 

Sorry for the trouble. Thank you for responding.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

PlayZot_Christopher

[GLOBAL: userInfoPane.html]
PlayZot_Christopher
  • Enthusiast

  • 51 posts
  • Corona SDK

I have a function that is supposed to create a json file in system.DocumentsDirectory to save level scores to if it doesn't exist, and if the file is already created to write over it with new correct level info if new levels were added after the file's initial creation.

engine_score["build"] = function()


        local ifscores = jcodec["exist"]("scores.json")
        local scorecard
        local readcard

        scorecard = { db = {} }

        if(ifscores==0)then

                jcodec["write"]("scores", scorecard)
                
        end

        readcard = jcodec["read2"]("scores.json", "db", "save")
       

        for i=1, #resLib["masterLib"]["map"] do
                if(readcard[i] == nil)then                
                        scorecard["db"][i] = 0
                elseif(readcard[i] ~= nil)then
                        scorecard["db"][i] = readcard[i]
                end
        
                composer.setVariable("ep" .. i .. "scr" , scorecard[i])

        end


        jcodec["write"]("scores", scorecard)


end

file writing function:


jcodec["write"] = function(fname, ftable)
            local savedData = ftable
            local tpath = fname .. ".json"
            local path = system.pathForFile(tpath, system.DocumentsDirectory)
            local file, err = io.open(path, "w")
            print(err)
            savedData = json.encode(ftable)
            file:write(savedData)
            io.close(file)
            file=nil
end

If scores.json doesn't exist in the folder, the functions doesn't give any errors. However, if scores.json already exists, I get an error from the write function for the line:

file:write(savedData)

The error claims that 'file' is nil. I tried printing err for setting file to io.open the path. The path is correct but err said that 'Permission Denied' to write to that file even though it exists and the path is correct. I tried opening it with "r+", "w+" but that didn't work either. So the error is occuring when I try to write, but the actual problem is I can't open the file for the purpose of writing, only reading. I am definitely closing the file every time after using it.

 

I tried to debug this as best I could but I have no idea what is wrong. My write function is more or less identical to the example code except I convert the table to json before trying to write the file. Removing that line didn't change anything either.

 

I tried moving the write function right before the 'read2' function and still the same effect.

 

Basically once the file is created, I can't write to it. I can't write over it with updated data. I don't know how to proceed.



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,380 posts
  • Corona SDK

I don't see an issue with the code immediately.  You may find it easier to simply take a working solution.

 

There are many ready-made solutions for saving and restoring tables using JSON encoding.
 

Here is mine: https://raw.githubusercontent.com/roaminggamer/SSK2/master/ssk2/core/extensions/table.lua

 

See table.save(), table.load() functions.

-- ==
--    table.save( theTable, fileName [, base ] ) - Saves table to file (Uses JSON library as intermediary)
-- ==
function table.save( theTable, fileName, base  )
   local base = base or  system.DocumentsDirectory
   local path = system.pathForFile( fileName, base )
   local fh = io.open( path, "w" )

   if(fh) then
      fh:write(json.encode( theTable ))
      io.close( fh )
      return true
   end   
   return false
end


-- ==
--    table.load( fileName [, base ] ) - Loads table from file (Uses JSON library as intermediary)
-- ==
function table.load( fileName, base )
   local base = base or system.DocumentsDirectory
   local path = system.pathForFile( fileName, base )
   if(path == nil) then return nil end
   local fh, reason = io.open( path, "r" )

   if( fh) then
      local contents = fh:read( "*a" )
      io.close( fh )
      local newTable = json.decode( contents )
      return newTable
   else
      return nil
   end
end

This is from my SSK2 lib: 



[TOPIC: post.html]
#3

PlayZot_Christopher

[GLOBAL: userInfoPane.html]
PlayZot_Christopher
  • Enthusiast

  • 51 posts
  • Corona SDK

  Best Answer

After further testing, I was able to make it work.

 

The source of the problem was that in at the top of the 'build' function, in the function jcodec["exist"], the file gets opened for reading but I forgot to close the file after it was done and nil it.

 

Sorry for the trouble. Thank you for responding.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: json, write