转自: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 = ;
//读取文件,注意使用参数"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;
}

最新文章

  1. APP接口自动化测试JAVA+TestNG(三)之HTTP接口测试实例
  2. Java8 Lambda表达式教程
  3. Windows Azure 虚拟机备份
  4. Python2.7<-------->Python3.x
  5. silverlight和wpf中暴露 给子类override
  6. iOS导航标题不居中问题(转载)
  7. Struts2第三篇【Action开发方式、通配符、Struts常量、跳转全局视图、action节点默认配置】
  8. 杨韬的Python/Jupyter学习笔记
  9. Xamarin.Android 调用手机拍照功能
  10. csharp: Use of Is and As operators in csharp
  11. gitbook editor教程
  12. 自动化测试:java + testng + maven + reportng + jenkins + selenium (一)_基于win环境
  13. C++ SUBLIME TEXT3 环境配置
  14. VB-创建类模块DLL文件
  15. LOJ#3048. 「十二省联考 2019」异或粽子(trie树+堆)
  16. 英国诗人乔叟Dethe is my Finaunce金融
  17. [Luogu5241]序列(DP)
  18. C#中Attribute和Property
  19. 2018.09.15 hdu3018Ant Trip(欧拉路)
  20. __iter__的有无

热门文章

  1. nodejs的调试(node-inspector)
  2. photoshop,用切片工具等分图片
  3. Sde表结构分析
  4. selenium + python 多浏览器测试
  5. 中文+django1.9+python3.5一些注意点
  6. 链接服务器"(null)"的 OLE DB 访问接口 "Microsoft.Jet.OLEDB.4.0" 返回了消息 "未指定的错误"。[手稿]
  7. linux_2015_0827_linux中一些常用词的发音and…
  8. motan源码解读:注册中心zookeeper(2)
  9. 【重读】The C++ Programming Language/C++编程语言(一)
  10. Web Farm和Web Garden的区别