File Open Error Detection on Lua

I use Lua on iOS, and I am having problems opening the file with io.open("filename.txt","w"), I know that I am getting zero, but is there any way to detect the cause of the failure and try to solve it to what? something like errno c?

+3
source share
1 answer

From the documentation :

io.open (filename [, mode])

This function opens the file in the modespecified string mode. It returns a new file descriptor or, in case of errors, nil plus an error message.

A usage example using the second value returned by the function is as follows:

local f, err = io.open("filename.txt", "w")
if f then
    -- do something with f
else
    print("Error opening file: " .. err)
end

If the process does not have permission to open the file, for example, the following message will be printed:

Error opening file: filename.txt: Permission denied

+7
source

All Articles