• Cocos2d-x3.0 文件处理



    1、从文件中读取

       auto sharedFileUtils = FileUtils::getInstance();
            std::string ret;
            sharedFileUtils->purgeCachedEntries();
            std::vector<std::string> searchPaths = sharedFileUtils->getSearchPaths();
            searchPaths.insert(searchPaths.begin(), "Misc");
            sharedFileUtils->setSearchPaths(searchPaths);
            
            std::vector<std::string> 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<std::string> 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);
                }
            }
    


    推断文件是否存在

            auto s = Director::getInstance()->getWinSize();
            auto sharedFileUtils = FileUtils::getInstance();
            
            Label* pTTF;
            bool isExist = false;
            
            //推断文件是否存在
            isExist = sharedFileUtils->isFileExist("grossini.png");
    
            pTTF = Label::createWithSystemFont(isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist", "", 20);
            pTTF->setPosition(Point(s.width / 2, s.height / 2));
            this->addChild(pTTF);
    



    auto s = Director::getInstance()->getWinSize();
            auto sharedFileUtils = FileUtils::getInstance();
            
            
            ValueMap dict;
            dict["grossini.bmp"] = Value("grossini.png");
            dict["grossini.xcf"] = Value("grossini.png");
            
            sharedFileUtils->setFilenameLookupDictionary(dict);
            auto sprite = Sprite::create("grossini.bmp");
            this->addChild(sprite);
            
            sprite->setPosition(Point(s.width / 2, s.height / 2));



    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    Tarjan求LCA
    过滤器、监听器、拦截器的区别
    java操作Redis缓存设置过期时间
    Redis和Memcached区别,Redis的过期策略
    缓存穿透、缓存击穿、缓存雪崩区别和解决方案
    Memcached和Redis在Linux下的安装
    jmeter学习
    用FastDFS一步步搭建文件管理系统
    version control
    关于Linux的防火墙命令和端口占用查询
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4663444.html
Copyright © 2020-2023  润新知