zoukankan      html  css  js  c++  java
  • Cocos2d-x项目移植到WP8系列之四:文件操作

     原文链接: http://www.cnblogs.com/zouzf/p/3972457.html

    读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建、删除、判断是否存在等。

    本来打算把把这部分代码放到C#工程来做,然后通过上一篇说到的C++和C#交互的那个通道来调用的,但是wp8里很多东西都被做成异步的形式了,文件的读写操作也被设计成了异步的形式,但是在C++这边发起调用的方法是需要同步调用的,这里如何转换是一个难题,由于对task PPL 那一套不熟悉,最后没办法只能放到C++工程来做。

    另外,在task里那个捕获异常也是很痛苦的一件事情——一直没研究透彻task,所以代码实现得比较挫

     1 bool WZFileUtilWP8::makeDirectory(const std::string& dirPath)
     2 {
     3 
     4     auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
     5     Platform::String ^pStr_FileName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(dirPath.c_str()).c_str());
     6 
     7     create_task(localFolder->CreateFolderAsync(pStr_FileName, CreationCollisionOption::ReplaceExisting)).wait();
     8 
     9     CCLog("craete folder: %s success
    ", dirPath.c_str());
    10     return true;
    11 
    12 }
    13 
    14 
    15 bool WZFileUtilWP8::removeDirectory(const std::string& dirName)
    16 {
    17     //return callCS->FunCallCS_RemoveDirectory(dirName.c_str());
    18 
    19     //删除不存在的文件夹会抛出异常,但这异常在task里捕获真TM蛋疼
    20     //现在通过先创建一个和要删除的文件夹同名的文件夹再删除来间接实现
    21 
    22     auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
    23     Platform::String ^pStr_dirName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(dirName.c_str()).c_str());
    24 
    25     this->makeDirectory(dirName);
    26 
    27     create_task(localFolder->GetFolderAsync(pStr_dirName)).then([](StorageFolder^ folder)
    28     {
    29         folder->DeleteAsync();
    30     }).wait();
    31 
    32     CCLog("remove folder: %s success
    ", dirName.c_str());
    33     return true;
    34 }
    35 
    36 
    37 bool WZFileUtilWP8::isFullPathExist(const std::string &fullPath)
    38 {
    39     auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
    40     Platform::String ^pStr_fulaPathName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(fullPath.c_str()).c_str());
    41     
    42     //还是异常捕获太蛋疼,直接创建吧,总是返回true
    43     create_task(localFolder->CreateFileAsync(pStr_fulaPathName, CreationCollisionOption::OpenIfExists)).wait();
    44 
    45     CCLog("file: %s is exist
    ", fullPath.c_str());
    46     return true;
    47 }
    48 
    49 
    50 
    51 //根据一个多级 目录 的字符串创建目录及文件
    52 bool WZFileUtilWP8::makeFullPath(const std::string& fullTargetPath)
    53 {
    54 
    55     //wp8可以直接创建多级目录
    56     //return callCS->FunCallCS_CreateFullPathFile(fullPath.c_str());
    57 
    58 
    59     auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
    60     Platform::String ^pStr_fullPathName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(fullTargetPath.c_str()).c_str());
    61     create_task(localFolder->CreateFileAsync(pStr_fullPathName, CreationCollisionOption::ReplaceExisting)).wait();
    62 
    63     CCLog("create file: %s success
    ", fullTargetPath.c_str());
    64 
    65     return true;  
    66 
    67 }

    原文链接: http://www.cnblogs.com/zouzf/p/3972457.html

  • 相关阅读:
    JSP总结1
    EL总结2-域
    EL总结1
    getRealPath和getContextPath
    mybatis获取参数数值的两个方式
    进程与线程区别与联系
    进程间通信和线程间通信的区别
    STRLEN
    二叉树数据结构和算法
    TYPDEF使用注意部分
  • 原文地址:https://www.cnblogs.com/zouzf/p/3972457.html
Copyright © 2011-2022 走看看