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

Cocos2d-x3.0 文件处理,布布扣,bubuko.com

时间: 2024-10-11 17:08:56

Cocos2d-x3.0 文件处理的相关文章

cocos2d(3.0)一些基础的东西

1.创建项目后环境配置: 附加文件夹:加入 $(EngineRoot) $(EngineRoot)cocos $(EngineRoot)cocos\editor-support $(EngineRoot)build\Debug.win32 ..\proj.win32 通用属性加入 (先从 cocos2d-x-3.0rc0 中 extensions   cocos\editor-support   cocos\ui 加入进去) libcocosstudio libExtensions libGUI

Ubuntu 16.04下使用gcc输出汇编的.0文件为可执行文件时出现:`_start&#39;被多次定义

错误如下: `_start'被多次定义 /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.text+0x0):第一次在此定义 /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:在函数‘_start’中: (.text+0x20):对‘main’未定义的引用 collect2: error: ld returned 1 exit sta

如何在Cocos2D 1.0 中掩饰一个精灵(六)

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰一个精灵:实现代码 打开HelloWorldLayer.m并且在init方法上方添加如下方法: - (CCSprite *)maskedSpriteWithSprite:(CCSprite *)textureSprite maskSprite:(CCSprite *)maskSprite { // 1 CCRenderTexture * rt = [CCRe

servlet3.0文件上传功能

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //获取文件部件part Part part = request.getPart("file"); String h = part.getHeader(&

jsp Servlet 3.0文件上传

Servlet 3.0之前上传文件一般都要借助与第三方插件上传,有了servlet3.0后,上传文件从此变得简单.老规矩,直接上代码. 1.建立一个index.jsp用于表单提交 1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3

VC++6.0文件关联问题的解决方法

最近我的电脑*.c文件关联失败,无法实现双击*.c打开vc++6.0,感觉特别不爽. 在经过自己的琢磨研究后,终于找到了解决方法. 特此分享下,希望可以帮到遇到同样问题的你. 核心内容: 1.&Open with MSDEV2."D:\Program Files\vc++6.0\Microsoft Visual Studio\Common\MSDev98\Bin\msdev.exe" "%1"3.Open("%1")4.msdev 按照下

Discuz X3核心文件解析

<?php /** *      [Discuz!] (C)2001-2099 Comsenz Inc. *      This is NOT a freeware, use is subject to license terms * *      $Id: discuz_application.php 34170 2013-10-28 02:58:29Z nemohou $ */ if(!defined('IN_DISCUZ')) { exit('Access Denied'); } clas

ACE6.2.0文件/目录操作

文件读取.#include "ace/FILE_Connector.h"#include "ace/FILE_IO.h"void fileRW(){ACE_FILE_Connector conn; ACE_FILE_IO f;conn.connect(f, ACE_FILE_Addr("d:\\list"), 0, ACE_Addr::sap_any, 0, O_RDWR|O_CREAT, ACE_DEFAULT_FILE_PERMS); cha

yii2.0 文件上传

Yii 2.0 出来好长时间了,一直都是看下官方网站,没实践过,今天弄了下图片上传操作. 1创建一个简单的数据表 mysql> desc article; +---------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+--------