加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

检测Lua上的文件打开错误

发布时间:2020-12-14 21:42:26 所属栏目:大数据 来源:网络整理
导读:我在iOS上使用Lua并且我在使用io.open(“filename.txt”,“w”)打开文件时遇到问题,我知道我收到了nil,但有没有办法检测到失败的原因并试图解决它?类似于错误的C? 解决方法 从 documentation: io.open (filename [,mode]) This function opens a file,in
我在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读