Cocos2d-x项目移植到WP8系列之四:文件操作
2014-09-15 11:18
274 查看
原文链接: /article/5080067.html
读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建、删除、判断是否存在等。
本来打算把把这部分代码放到C#工程来做,然后通过上一篇说到的C++和C#交互的那个通道来调用的,但是wp8里很多东西都被做成异步的形式了,文件的读写操作也被设计成了异步的形式,但是在C++这边发起调用的方法是需要同步调用的,这里如何转换是一个难题,由于对task PPL 那一套不熟悉,最后没办法只能放到C++工程来做。
另外,在task里那个捕获异常也是很痛苦的一件事情——一直没研究透彻task,所以代码实现得比较挫
原文链接: /article/5080067.html
读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建、删除、判断是否存在等。
本来打算把把这部分代码放到C#工程来做,然后通过上一篇说到的C++和C#交互的那个通道来调用的,但是wp8里很多东西都被做成异步的形式了,文件的读写操作也被设计成了异步的形式,但是在C++这边发起调用的方法是需要同步调用的,这里如何转换是一个难题,由于对task PPL 那一套不熟悉,最后没办法只能放到C++工程来做。
另外,在task里那个捕获异常也是很痛苦的一件事情——一直没研究透彻task,所以代码实现得比较挫
bool WZFileUtilWP8::makeDirectory(const std::string& dirPath) { auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder; Platform::String ^pStr_FileName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(dirPath.c_str()).c_str()); create_task(localFolder->CreateFolderAsync(pStr_FileName, CreationCollisionOption::ReplaceExisting)).wait(); CCLog("craete folder: %s success\n", dirPath.c_str()); return true; } bool WZFileUtilWP8::removeDirectory(const std::string& dirName) { //return callCS->FunCallCS_RemoveDirectory(dirName.c_str()); //删除不存在的文件夹会抛出异常,但这异常在task里捕获真TM蛋疼 //现在通过先创建一个和要删除的文件夹同名的文件夹再删除来间接实现 auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder; Platform::String ^pStr_dirName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(dirName.c_str()).c_str()); this->makeDirectory(dirName); create_task(localFolder->GetFolderAsync(pStr_dirName)).then([](StorageFolder^ folder) { folder->DeleteAsync(); }).wait(); CCLog("remove folder: %s success\n", dirName.c_str()); return true; } bool WZFileUtilWP8::isFullPathExist(const std::string &fullPath) { auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder; Platform::String ^pStr_fulaPathName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(fullPath.c_str()).c_str()); //还是异常捕获太蛋疼,直接创建吧,总是返回true create_task(localFolder->CreateFileAsync(pStr_fulaPathName, CreationCollisionOption::OpenIfExists)).wait(); CCLog("file: %s is exist\n", fullPath.c_str()); return true; } //根据一个多级 目录 的字符串创建目录及文件 bool WZFileUtilWP8::makeFullPath(const std::string& fullTargetPath) { //wp8可以直接创建多级目录 //return callCS->FunCallCS_CreateFullPathFile(fullPath.c_str()); auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder; Platform::String ^pStr_fullPathName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(fullTargetPath.c_str()).c_str()); create_task(localFolder->CreateFileAsync(pStr_fullPathName, CreationCollisionOption::ReplaceExisting)).wait(); CCLog("create file: %s success\n", fullTargetPath.c_str()); return true; }
原文链接: /article/5080067.html
相关文章推荐
- [图文]为移植到Android平台上的Cocos2d-x项目添加xml布局文件
- Cocos2d-x项目移植到WP8系列之二:开篇
- Cocos2d-x项目移植到WP8系列之一:前传
- Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行
- Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL
- Cocos2d-x项目移植到WP8系列之七:中文显示乱码
- Cocos2d-x项目移植到WP8系列之三:C++和C#的交互
- Cocos2d-x项目移植到WP8系列之五:播放MP3
- Cocos2d-x项目移植到WP8系列之九:使用自定义shader
- 一个项目中使用到的通用函数库(3) 文件IO操作!
- Linux命令-基本操作及文件操作常用命令--详解Linux入门到精通系列
- My Silverlight系列(4)——恼人的Silverlight文件操作
- 文件操作系列之四——(CFile类的文件操作)
- 文件操作系列之二——(标准C++文件流)
- 操作 Wave 文件(3): 接触 mmio 系列函数
- 文件操作系列之六——(CArchive的文件操作)
- TC官方文档翻译11----文件操作API(Tokyo Cabinet/Tokyo Tyarnt 文档系列)
- 文件操作系列之一——(C Run-Time Library)
- C#文件操作 --【C#网文收集系列】
- C#文件操作 --【C#网文收集系列】