Cocos2d-Lua之文件操作

来源:互联网 发布:房屋设计软件下载 编辑:程序博客网 时间:2024/05/21 07:12

一、文件打开操作

格式:

file, msg = io.open("文件的绝对路径名", "打开方式")   --打开方式有r, w, a, r+, w+, a+,跟C/C++的打开方式是一样的,返回文件和是否正确打开的信息

二、文件读取方式

格式:

file:read("*line")    --读取文件中的一行,若缺省默认也是读取一行file:read("*all")       --读取文件中的全部内容

三、文件的写入

格式:

file:write("要写入的内容")

四、示例

function fileRead()    file, msg = io.open("D:\\test\\MyGame\\firstgame\\src\\app\\scenes\\filetest.txt", "r")    if not file then        print(msg)    end    --s = file:read("*all")    repeat        s = file:read()        if(s ~= nil) then            print(s)        else            break;        end    until false    print(s)endfunction fileWrite()    file, msg = io.open("D:\\test\\MyGame\\firstgame\\src\\app\\scenes\\filetest.txt", "a")    file:write("hello world\n")    file:flush()    --刷新文件    file:close()    --关闭文件endfileWrite()    --先写入文件fileRead()      --再读取文件

输出结果:

这里写图片描述

这里地方打印出了nil,有待考究。同样可以用file:read(“*all”)一次性全部读取出来。

0 0
原创粉丝点击