Cocos2d-x3.0 文件处理

来源:未知 责任编辑:责任编辑 发表时间:2014-05-20 18:26 点击:


1、从文件中读取内容

   auto sharedFileUtils = FileUtils::getInstance();
        std::string ret;
        sharedFileUtils->purgeCachedEntries();
        std::vector searchPaths = sharedFileUtils->getSearchPaths();
        searchPaths.insert(searchPaths.begin(), "Misc");
        sharedFileUtils->setSearchPaths(searchPaths);
        
        std::vector resolutionsOrder = sharedFileUtils->getSearchResolutionsOrder();
        resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd");
        resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad");
        resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd");
        resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide");
        resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd");
        resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone");

        
        sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder);
        
        for (int i = 1; i < 7; i++) {
            auto filename =StringUtils::format("test%d.txt",i);
            //获取文件路径
            ret = sharedFileUtils->fullPathForFilename(filename.c_str());
            //获取文件中的字符串
            std::string st = FileUtils::getInstance()->getStringFromFile(filename);
            log("%s -> %s",filename.c_str(),st.c_str());
        }

向本地写入和读取

        auto sharedFileUtils = FileUtils::getInstance();
        std::string ret;
    
        sharedFileUtils->purgeCachedEntries();
        std::vector searchPaths = sharedFileUtils->getSearchPaths();
        std::string writablePath = sharedFileUtils->getWritablePath();
        std::string fileName = writablePath + "external1.txt";
        char szBuf[100] = "Hello Cocos2d-xrewrwe!";
        /*向本地写入*/
        FILE* fp = fopen(fileName.c_str(), "wb");
        if (fp) {
            size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp);
            CCASSERT(ret != 0, "fwrite function returned zero value");
            fclose(fp);
            if (ret != 0)
                log("Writing file to writable path succeed.");
            
        }
        
        
        /**********************************/
        searchPaths.insert(searchPaths.begin(), writablePath);
        sharedFileUtils->setSearchPaths(searchPaths);
        
        
        //Get external.txt from writable path
        std::string fullPath = sharedFileUtils->fullPathForFilename("external1.txt");
        log("external file path = %s",fullPath.c_str());
        /*读取文件*/
        if (fullPath.length() > 0) {
            fp = fopen(fullPath.c_str(), "rb");
            if (fp) {
                char szReadBuf[100] = {0};
                size_t read = fread(szReadBuf, 1, strlen(szBuf), fp);
                if (read > 0)
                    log("The content of file from writable path: %s",szReadBuf);
                fclose(fp);
            }
        }

	
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • cocos2d-x学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x学习(一):HelloWorld
  • cocos2dx在xcode下开发,编译到android上(2)
  • cocos2d 设置屏幕默认方向
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1