cocos2d-x 二进制文件的读写

cocos2d-x 二进制文件的读写,第1张

概述转自:http://blog.csdn.net/wolfking_2009/article/details/10616069 cocos2d-x里面的二进制文件读取的方法是有的,作者对方法封装了下,将读取的路径设置到了writablePath路径上,这样方便读取自己存储的二进制文件。作者在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,对于没有找到文件是否d出提示框的设置      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;    } 
总结

以上是内存溢出为你收集整理的cocos2d-x 二进制文件的读写全部内容,希望文章能够帮你解决cocos2d-x 二进制文件的读写所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1006411.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存