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

cocos2d-x 3.2文件读写(普通文件,plist,xml)

发布时间:2020-12-14 19:22:35 所属栏目:百科 来源:网络整理
导读:一.普通文件读写 1.写入文件( bool HelloWorld ::init()里写入如下代码) FileUtils *fu=FileUtils::getInstance(); FILE *f=fopen(fu-fullPathFromRelativeFile("data.txt",fu-getWritablePath()).c_str(),"w"); fprintf(f,"Hello wsn"); fclose(f); log(

一.普通文件读写

1.写入文件(bool HelloWorld::init()里写入如下代码)

   FileUtils *fu=FileUtils::getInstance();
    FILE *f=fopen(fu->fullPathFromRelativeFile("data.txt",fu->getWritablePath()).c_str(),"w");
    fprintf(f,"Hello wsn");
    fclose(f);
    log("%s",fu->getWritablePath().c_str());

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

3.UserDefault的用法
    UserDefault::getInstance()->setStringForKey("data","Hello ws");
    
    log("%s",UserDefault::getInstance()->getStringForKey("data1","Hello world").c_str());


二.读取plist文件

1.先在resource里创建plist文件


2.然后在HelloWorldScene.cpp的init()函数输入以下代码即可

    FileUtils *fu=FileUtils::getInstance();
    auto vm=fu->getValueMapFromFile("data.plist");//dictionary,如果是vector,使用fu->getValueVectorFromFile(const std::string &filename)
    
    log("%s",vm["name"].asString().c_str());
    log("%s",vm["arr"].asValueVector().at(1).asString().c_str());

三.读取xml文件

1.先在resource里创建xml文件,在里面输入

<data>
    <p name="ZhangSan" age="10" />
    <p name="LiSi" age="11" />
</data>

2.输入以下代码
  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());
    }

(编辑:李大同)

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

    推荐文章
      热点阅读