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

cocos2d-x文件读写、Json操作

发布时间:2020-12-14 16:48:39 所属栏目:百科 来源:网络整理
导读:文件读写操作 // 1. 获取可操作的程序文件夹 std ::string path1 = FileUtils ::getInstance () - getWritablePath()+ "user.txt" ; CCLOG(path1.c_str()); // C : /Users/laixiao/AppData/Local/Test3/ // 2. 写入数据到文件 std ::string str0 = "laixiao

文件读写操作


 //1.获取可操作的程序文件夹 std::string path1 = FileUtils::getInstance()->getWritablePath()+"user.txt"; CCLOG(path1.c_str());//C:/Users/laixiao/AppData/Local/Test3/ //2.写入数据到文件 std::string str0 = "laixiao赖肖";//会有中文乱码(cocos2d-x默认utf-8编码;可是内容是gb2312编码) FileUtils::getInstance()->writeStringToFile(str0,path1); //3.从文件读取一个字符串 std::string str1 = FileUtils::getInstance()->getStringFromFile(path1); CCLOG(str1.c_str()); //一:反序列化过程 /* 使用#include "spine/Json.h" 新建f1.txt文件,json格式的内容,保存到Data/f1.txt [ {"id":10000,"name":"记得"},{"id":10001,"name":"用"},{"id":10002,"name":"utf-8无Bom格式"},{"id":10003,"name":"编码"},{"id":10004,"name":"保存到工程目录"},"name":"Resource文件夹res文件夹下"},] */ //直接读取工程目录下的文件 std::string str2 = FileUtils::getInstance()->getStringFromFile("Data/f1.txt"); CCLOG(str2.c_str()); //字符串转换为json Json* f1Json = Json_create(str2.c_str()); //获取Json数组的一个元素 Json* node = f1Json->child; while (node) { //获取id,转换为int类型 int id = Json_getItem(node,"id")->valueInt; //获取id,转换为String类型 std::string name = Json_getItem(node,"name")->valueString; CCLOG(name.c_str()); node = node->next; } //二:序列化过程 std::string str3 = "{"id":1000,"name":"laixiao"}"; CCLOG(str3.c_str());

(编辑:李大同)

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

    推荐文章
      热点阅读