• cocos2d-x 二进制文件的读写


    转自:http://blog.csdn.net/wolfking_2009/article/details/10616069

    cocos2d-x里面的二进制文件读取的方法是有的,作者对方法封装了下,将读取的路径设置到了writablePath路径上,这样方便读取自己存储的二进制文件。作者在cocos2d-x中没有找到二进制文件输出的方法,于是自己写了一个。下面就是两个方法的源码实现:

    二进制文件的读取:

    unsigned char* wkFileUtils::getFileByName(string pFileName){  
        //记录cocos2d-x中CCFileUtils,对于没有找到文件是否弹出提示框的设置
        bool isNeedModifyPopupSetting  = CCFileUtils::sharedFileUtils()->isPopupNotify();
        //如果有提示,就暂时关闭,因为这里的读取可能找不到该文件,因为该文件有可能还没有创建
        if(isNeedModifyPopupSetting)
        {
            CCFileUtils::sharedFileUtils()->setPopupNotify(false);
        } 
        //获取文件的路径,使用getWritablePath是因为这个文件是我们需要存储的文件
        string path = CCFileUtils::sharedFileUtils()->getWritablePath() + pFileName; 
        CCLog("path = %s",path.c_str());
        unsigned long len = 0;
        //读取文件,注意使用参数"rb",r表示read,b表示二进制binary
        unsigned char* data = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(), "rb", &len);
        CCLog("read data length = %d", len);
        //如果以前设置找不到文件有提示,则改回原来的设置
        if(isNeedModifyPopupSetting)
        {
            CCFileUtils::sharedFileUtils()->setPopupNotify(true);
        }
        return data;  
    }

    二进制文件的写入:

    bool wkFileUtils::saveFile(unsigned char *pContent, string pFileName, int length){  
        //获取储存的文件路径 
        string path = CCFileUtils::sharedFileUtils()->getWritablePath() + pFileName;  
        CCLog("save file path = %s",path.c_str());  
    
        //创建一个文件指针,注意要使用参数"wb",w表示write,b表示二进制binary,之前我使用的是"w",ios上当时没有发现问题,但是win32上会有bug,改成"wb"就没有问题了
        FILE* file = fopen(path.c_str(), "wb"); 
    
        if (file) { 
            fwrite(pContent, sizeof(unsigned char), length, file);
            fclose(file);  
        }  
        else
        {
            CCLog("save file error.");  
        }  
        return false;  
    }
  • 相关阅读:
    jQuery中的Deferred详解和使用
    Windows Server 2003 无法布署.NET Framework4.5 应用解决方案
    jquery 获取父窗口的元素、父窗口、子窗口
    Android USB HOST API
    ASP.NET MVC+Vue.js实现联系人管理
    Vue在ASP.NET MVC中的进行前后端的交互
    JS中 “is not defined” 如何判断defined,defined和undefined 的区别
    Android自定义view实现个人中心设置界面带点击事件
    java 正则表达式(内附例子)
    在asp.net mvc 中使用Autofac
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/3645114.html
Copyright © 2020-2023  润新知