cocos2d-x&android从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;
        }
    }
上一篇:android – 如何使用Locale和Configuration选择不同的资产文件作为我的语言更改?


下一篇:BEST FREE UNITY ASSETS – OVER 200 CURATED QUALITY ASSETS