Cocos2d-Lua之文件操作
2016-06-17 20:18
435 查看
一、文件打开操作
格式:
二、文件读取方式
格式:
三、文件的写入
格式:
四、示例
输出结果:
这里地方打印出了nil,有待考究。同样可以用file:read(“*all”)一次性全部读取出来。
格式:
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) end function fileWrite() file, msg = io.open("D:\\test\\MyGame\\firstgame\\src\\app\\scenes\\filetest.txt", "a") file:write("hello world\n") file:flush() --刷新文件 file:close() --关闭文件 end fileWrite() --先写入文件 fileRead() --再读取文件
输出结果:
这里地方打印出了nil,有待考究。同样可以用file:read(“*all”)一次性全部读取出来。
相关文章推荐
- Cocos2d-Lua之math库
- Cocos2d-Lua之随机数
- cocos2dx 渲染任意形状贴图
- 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法
- windows cocos2d-x OpenGL error 0x0506 quick-cocos2d-x\lib\cocos2d-x\cocos2dx\sprite_nodes\ccsprite.c
- Cocos2d-Lua之string
- cocos2dx 3.8加载cocos studio csb文件
- cocos2D开发游戏
- Cocos2d-Lua之table的排序
- Cocos2d-Lua之Lua文件的加载与运行
- cocos2d-x面试题合集
- CocosPosd安装,错误
- Cocos2d-Lua之table的删除与插入
- cocos2dx 一张图片的闪烁的动画
- Cocos2d-Lua之table的遍历
- Cocos2d-Lua之table
- 如何升级cocos2d-x来支持ipv6以及socket怎么支持ipv6
- cocoapods 安装写使用
- Cocos2d-x 3.0 编译出错 解决 error: expected '' at end of member declaration
- Cocos2dx 中的CCCallFunc,CCCallFuncN,CCCallFuncND,CCCallFuncO比较