cocos2dx android fopen读取文件失败

cocos2dx android fopen读取文件失败,第1张

概述 在cocos2dx中,提供了CCFileUitl来进行文件 *** 作,但是大家一般习惯的方式还是仅仅通过这个类获取路径,然后用fopen相关的函数来 *** 作,大概如下: [cpp]  view plain copy string fullPath = fullPathForFilename(pszFileName);   FILE *fp = fopen(fullPath.c_str(), pszMode

在cocos2dx中,提供了CCfileUitl来进行文件 *** 作,但是大家一般习惯的方式还是仅仅通过这个类获取路径,然后用fopen相关的函数来 *** 作,大概如下:

[cpp] view plain copy @H_403_11@ @H_403_11@ @H_403_11@ stringfullPath=fullPathForfilename(pszfilename); file*fp=fopen(fullPath.c_str(),pszMode); @H_403_11@
但是这样,windows和iOS正常无误,但是在androID下面,会读取不到文件。之前遇到这个问题,没有解决,后面为了统一,改成了CCfileUtil的getfileData就没有出现过,也没有继续去追。

今天追了之后,发现,在androID下,CCfileUtil有独立实现,在cocos2d-x-2.2.3/cocos2dx/platform/androID/下面(引擎版本2.2.3),CCfileUtilsAndroID。其中getfileData的实现如下:

unsignedchar*CCfileUtilsAndroID::getfileData(constchar*pszfilename,char*pszMode,unsignedlong*pSize) { returndoGetfileData(pszfilename,pszMode,pSize,153);Font-weight:bold;background-color:inherit;">false); } unsignedchar*CCfileUtilsAndroID::getfileDataForAsync(true); unsignedchar*CCfileUtilsAndroID::doGetfileData(long*pSize,boolforAsync) { char*pData=0; if((!pszfilename)||(!pszMode)||0==strlen(pszfilename)) return0; } stringfullPath=fullPathForfilename(pszfilename); if(fullPath[0]!='/') if(forAsync) {/**********!!!注意啊***********/ pData=s_pZipfile->getfileData(fullPath.c_str(),s_pZipfile->_dataThread); else else do CC_BREAK_IF(!fp); longsize; fseek(fp,SEEK_END); size=ftell(fp); pData=newunsignedchar[size]; size=fread(pData,sizeof(unsignedchar),size,fp); fclose(fp); if(pSize) *pSize=size; }while(0); if(!pData) std::stringmsg="Getdatafromfile("; msg.append(pszfilename).append(")Failed!"); cclOG("%s",msg.c_str()); returnpData; } @H_403_11@ 注意 上面代码里面我标记注释的地方, 有个东西叫s_pZipfile ,之前看到这里,没注意这个东西。仔细一看,前面的fullpath的返回值,是一个相对于asset/的文件,也就是说,基本上都会走这个if,那么这个s_pZipfile又是怎么定义的。

#include"support/zip_support/ZipUtils.h" //recordthezipontheresourcepath staticZipfile*s_pZipfile=NulL; CCfileUtils*CCfileUtils::sharedfileUtils() if(s_sharedfileUtils==NulL) s_sharedfileUtils=newCCfileUtilsAndroID(); s_sharedfileUtils->init(); std::stringresourcePath=getApkPath(); s_pZipfile=newZipfile(resourcePath,"assets/"); returns_sharedfileUtils; 重要的头文件我都留在上面了,所以其实上面那个getfileData的实现是这样的

char*Zipfile::getfileData(conststd::string&filename,ZipfilePrivate*data) char*pBuffer=NulL; if(pSize) *pSize=0; do CC_BREAK_IF(!data->zipfile); CC_BREAK_IF(filename.empty()); ZipfilePrivate::fileListContainer::const_iteratorit=data->fileList.find(filename); CC_BREAK_IF(it==data->fileList.end()); ZipEntryInfofileInfo=it->second; intnRet=unzGoTofilePos(data->zipfile,&fileInfo.pos); CC_BREAK_IF(UNZ_OK!=nRet); nRet=unzOpenCurrentfile(data->zipfile); CC_BREAK_IF(UNZ_OK!=nRet); pBuffer=char[fileInfo.uncompressed_size]; intCC_UNUSednSize=unzReadCurrentfile(data->zipfile,pBuffer,fileInfo.uncompressed_size); CCAssert(nSize==0||nSize==(int)fileInfo.uncompressed_size,"thefilesizeiswrong"); *pSize=fileInfo.uncompressed_size; unzCloseCurrentfile(data->zipfile); }while(0); returnpBuffer; } @H_403_11@ 基本上到这,问题就清楚了, 总结起来就是2点

一、assets其实是一个zip压缩文件,直接读取里面的内容是不行的。

二、androID的实现和其他2个平台不一样。


解决办法:

最简单的,直接用CCfileUtil的getfileData实现文件读取。

不然就从上面提到的文件去改。。。



其他方法,参考:1、http://www.cppblog.com/johndragon/archive/2012/12/28/196754.HTML

2、http://blog.csdn.net/happyhell/article/details/7414110

(原文地址:http://blog.csdn.net/dinko321/article/details/41309735)

@H_403_11@ 总结

以上是内存溢出为你收集整理的cocos2dx android fopen读取文件失败全部内容,希望文章能够帮你解决cocos2dx android fopen读取文件失败所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1023652.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存