微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;    
} 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐