1

I'm struggling with a permission error in Lua when trying to read/write from/to a text file. As you can see below, I've pulled the error message from the io.open function and I'm getting "file.txt: permission denied". If it helps at all, I'm using Mac OSX Yosemite and the Love2D engine.

function fileWrite()
    outputFile, error = io.open("new.txt", "w")
    if outputFile then
        for k,v in pairs(clicks) do
            outputFile:write(tostring(v[1]) .. "," .. tostring(v[2]) .. "\n")
        end
        outputFile:close()
    else
        errorText = error
    end
end

Am I making a silly error somewhere by any chance? I've dealt with writing to files in Lua before (on Windows 7), and I've never had this problem before.

Any feedback would be greatly appreciated! :)

cmimm101
  • 37
  • 6

2 Answers2

3

In LÖVE your game shouldn't be interacting directly with the file system through io. Instead uselove.filesystem.newFile so your assets will still be available within a .love (zip) file. This should also handle the permission issues your having on OS X as it will write to /Users/user/Library/Application Support/LOVE/ to which love will have write permissions.

function fileWrite()
    outputFile, error = love.filesystem.newFile("new.txt")
    if outputFile:open("w") then
        outputFile:write("Hello World!")
        outputFile:close()
    else
        print(error)
    end
end
ryanpattison
  • 6,151
  • 1
  • 21
  • 28
0

Check your current directory. For OS X and Linux like systems:

require "os"
print( os.getenv("PWD") )

You do not have access to the file system where the application is running.

ErnieE
  • 143
  • 1
  • 6
  • I tried that, and it returned a nil value for some reason. The folder that contains this code, however, is located directly under the Home folder (in my case "chris"). Is there any way to change the access levels of this file system? – cmimm101 Jan 07 '15 at 23:50