cocos2d-x 二进制文件的读写

来源:互联网 发布:mysql 字符转日期函数 编辑:程序博客网 时间:2024/06/01 23:55

转自: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;  }
复制代码

0 0