ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

cocos2d-x&android从assets文件夹加载文件

2019-08-31 00:26:03  阅读:208  来源: 互联网

标签:cocos2d-x android c assets


我在assets / plist /文件夹中有一堆plist文件,我正在尝试加载这些文件来验证它们的哈希值.

会发生什么是以下代码对我失败

const char *fullPath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(name).c_str();
std::ifstream ifs(fullPath, std::ios::binary);
std::vector<char> str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

返回的char数组始终为空.

尝试使用fopen打开同一个文件也会导致文件句柄的空指针.

我已经验证完整路径是assets / plists / file.plist,并且file.plist存在于assets / plist文件夹中.

我在这做错了什么?

解决方法:

FileUtils-> getInstance() – > getFileData是我阅读资产资源的方式.我在阅读文本文件时将其包装在实用程序函数中:

    #include "cocos2d.h"
    #include <iosfwd>
    #include <sstream>
    #include <memory>

    namespace FileUtil
    {
        using ResourceStream = std::basic_istringstream<char>;

        bool readResourceFile(std::shared_ptr<ResourceStream>& stream,const std::string& filename);

        bool readResourceFile(std::shared_ptr<ResourceStream>& stream,const std::string& filename)
        {
            // Note: Returned data allocated by "malloc" so must free when copy to string stream

            CCLOG("FileUtil::readResourceFile - Attempting to read resource file %s",filename.c_str());

            ssize_t size = 0;
            char* data = reinterpret_cast<char*>(FileUtils::getInstance()->getFileData(filename, "r", &size));
            if(!data || size == 0)
            {
                CCLOG("FileUtil::readResourceFile - unable to read filename %s - size was %lu",filename.c_str(),size);
                if(data)
                {
                    free(data);
                }
                return false;
            }

            CCLOG("FileUtil::readResourceFile - Read %lu bytes from resource file %s",size,filename.c_str());

            std::string stringData(data);
            // release since we've copied to string
            free(data);

            stream.reset(new std::istringstream(stringData));

            return true;
        }
    }

标签:cocos2d-x,android,c,assets
来源: https://codeday.me/bug/20190830/1771463.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有