博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cocos2d-x3.0 文件处理
阅读量:5050 次
发布时间:2019-06-12

本文共 3888 字,大约阅读时间需要 12 分钟。

1、从文件中读取

auto sharedFileUtils = FileUtils::getInstance();        std::string ret;        sharedFileUtils->purgeCachedEntries();        std::vector
searchPaths = sharedFileUtils->getSearchPaths(); searchPaths.insert(searchPaths.begin(), "Misc"); sharedFileUtils->setSearchPaths(searchPaths); std::vector
resolutionsOrder = sharedFileUtils->getSearchResolutionsOrder(); resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); for (int i = 1; i < 7; i++) { auto filename =StringUtils::format("test%d.txt",i); //获取文件路径 ret = sharedFileUtils->fullPathForFilename(filename.c_str()); //获取文件里的字符串 std::string st = FileUtils::getInstance()->getStringFromFile(filename); log("%s -> %s",filename.c_str(),st.c_str()); }

向本地写入和读取

auto sharedFileUtils = FileUtils::getInstance();        std::string ret;            sharedFileUtils->purgeCachedEntries();        std::vector
searchPaths = sharedFileUtils->getSearchPaths(); std::string writablePath = sharedFileUtils->getWritablePath(); std::string fileName = writablePath + "external1.txt"; char szBuf[100] = "Hello Cocos2d-xrewrwe!"; /*向本地写入*/ FILE* fp = fopen(fileName.c_str(), "wb"); if (fp) { size_t ret = fwrite(szBuf, 1, strlen(szBuf), fp); CCASSERT(ret != 0, "fwrite function returned zero value"); fclose(fp); if (ret != 0) log("Writing file to writable path succeed."); } /**********************************/ searchPaths.insert(searchPaths.begin(), writablePath); sharedFileUtils->setSearchPaths(searchPaths); //Get external.txt from writable path std::string fullPath = sharedFileUtils->fullPathForFilename("external1.txt"); log("external file path = %s",fullPath.c_str()); /*读取文件*/ if (fullPath.length() > 0) { fp = fopen(fullPath.c_str(), "rb"); if (fp) { char szReadBuf[100] = {0}; size_t read = fread(szReadBuf, 1, strlen(szBuf), fp); if (read > 0) log("The content of file from writable path: %s",szReadBuf); fclose(fp); } }

推断文件是否存在

auto s = Director::getInstance()->getWinSize();        auto sharedFileUtils = FileUtils::getInstance();                Label* pTTF;        bool isExist = false;                //推断文件是否存在        isExist = sharedFileUtils->isFileExist("grossini.png");        pTTF = Label::createWithSystemFont(isExist ? "Images/grossini.png exists" : "Images/grossini.png doesn't exist", "", 20);        pTTF->setPosition(Point(s.width / 2, s.height / 2));        this->addChild(pTTF);

auto s = Director::getInstance()->getWinSize();        auto sharedFileUtils = FileUtils::getInstance();                        ValueMap dict;        dict["grossini.bmp"] = Value("grossini.png");        dict["grossini.xcf"] = Value("grossini.png");                sharedFileUtils->setFilenameLookupDictionary(dict);        auto sprite = Sprite::create("grossini.bmp");        this->addChild(sprite);                sprite->setPosition(Point(s.width / 2, s.height / 2));

版权声明:本文博客原创文章。博客,未经同意,不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4663444.html

你可能感兴趣的文章
Maven中classifier
查看>>
Use Laravel/homestead 环境维护基于 brophp 开发的老项目
查看>>
测绘软件优缺点
查看>>
SQL优化(SQL TUNING)可大幅提升性能的实战技巧之一——让计划沿着索引跑
查看>>
pyhton自动化项目编码以及命名规范
查看>>
会声会影毛玻璃制作
查看>>
webmagic循环爬取
查看>>
用UltraEdit比較两个文件
查看>>
CCAccelerometerDelegate
查看>>
cocos2d-x游戏开发系列教程-坦克大战游戏之坦克和地图碰撞的检测下
查看>>
简单的sql注入
查看>>
c3p0连接池和dbutils的使用
查看>>
崩溃。。。
查看>>
POJ 2459 模拟
查看>>
unix basic command
查看>>
Spring MVC 自定义类型转换器
查看>>
ECS服务器搭建Discuz 邮箱设置,报错处理
查看>>
RHEL 6.5---SVN服务实现过程
查看>>
Install KR3 for AX SP3
查看>>
一个前端开发者换电脑的过程(git篇)
查看>>