cocos 读写操作

该段代码展示了在Cocos2d-x游戏引擎中,针对iOS和非iOS平台进行文件读写操作的方法。在iOS上直接返回文件全路径,而在其他平台上,首先检查可写路径下文件是否存在,若存在则返回,否则尝试从资源路径复制文件到可写路径,并写入。如果写入成功,则返回可写路径,否则记录错误并返回空字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS

    auto xx_imgs = FileUtils::getInstance()->fullPathForFilename(filename);

    return xx_imgs;

#else

    // 资源路径

    std::string sourcePath = cocos2d::FileUtils::getInstance()->fullPathForFilename(filename);

    // 可写路径

    std::string destPath = cocos2d::FileUtils::getInstance()->getWritablePath() + filename;

    if (cocos2d::FileUtils::getInstance()->isFileExist(destPath)) {

        return destPath;

    }

    if (cocos2d::FileUtils::getInstance()->isFileExist(destPath + ".tmp")) {

        cocos2d::FileUtils::getInstance()->removeFile(destPath + ".tmp");

    }

    

    // write

    cocos2d::Data data = cocos2d::FileUtils::getInstance()->getDataFromFile(sourcePath);

    FILE *fp = fopen(destPath.c_str(), "w+");

    if (fp)

    {

        size_t size = fwrite(data.getBytes(), sizeof(unsigned char), data.getSize(), fp);

        fclose(fp);

        

        if (size > 0)

        {

            return destPath;

        }

    }

    CCLOG("copy file %s failed.", filename.c_str());

    return "";

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值