Cocos2dx 3.1.1 之 数据存储

来源:互联网 发布:查看ubuntu版本命令 编辑:程序博客网 时间:2024/05/08 13:01

首选项存储:

bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }//存储数据UserDefault::getInstance()->setStringForKey("data", "Hello World!");//取出并打印数据//根据key("data")获取数据,如果找不到则输出失败值"can not find!"log("%s", UserDefault::getInstance()->getStringForKey("data", "can not find!").c_str());    return true;}



读写文件:

写文件:

    auto fu = FileUtils::getInstance();    FILE *f = fopen(fu->fullPathFromRelativeFile("data.txt", fu->getWritablePath()).c_str(), "w");    fprintf(f, "Hello World!\n");    fclose(f);


读文件:

    Data d = fu->getDataFromFile(fu->fullPathFromRelativeFile("data.txt", fu->getWritablePath()));        log("%s",d.getBytes());



读取plist文件

FileUtils *fu = FileUtils::getInstance();ValueMap vm = fu->getValueMapFromFile("data.plist");log("%s",vm["name"].asString().c_str());


读取xml文件

#include "tinyxml2.h"

    auto doc = new tinyxml2::XMLDocument();    doc->Parse(FileUtils::getInstance()->getStringFromFile("data.xml").c_str());    auto root = doc->RootElement();        for (auto e = root->FirstChildElement(); e; e=e->NextSiblingElement()) {                std::string str;                for (auto attr = e->FirstAttribute(); attr; attr=attr->Next()) {            str+=attr->Name();            str+=":";            str+=attr->Value();            str+=",";        }                log("%s",str.c_str());    }



0 0