检测Lua上的文件打开错误

我在iOS上使用Lua并且我在使用io.open(“filename.txt”,“w”)打开文件时遇到问题,我知道我收到了nil,但有没有办法检测到失败的原因并试图解决它?类似于错误的C?

解决方法

documentation

io.open (filename [,mode])

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

使用函数返回的第二个值的示例用法如下:

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

例如,如果进程没有打开文件的权限,则会打印出以下消息:

Error opening file: filename.txt: Permission denied

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...