Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x文件读写、Json操作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

文件读写操作


 //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());

大佬总结

以上是大佬教程为你收集整理的cocos2d-x文件读写、Json操作全部内容,希望文章能够帮你解决cocos2d-x文件读写、Json操作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。