Cocos2D文件读取I/O

参照了别人的程序,修改了Path:
  1. //string path = CCFileUtils::sharedFileUtils()->getWriteablePath()+pFileName ;
  2.         string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pFileName.c_str());
复制代码

通过fullPathFromRelativePath, 看名称,好像要你输入相对路径,其实你只需要输入Resource文件夹里的某个文件名,它就会识别这个文件的路径。所以你要传入的不是路径,而是参数,官方API文档误导人。

  1. const char* fullPathFromRelativePath        (        const char *         pszRelativePath        )         
  2. Generate the absolute path of the file.
  3. Parameters
  4. pszRelativePath        The relative path of the file.
  5. Returns
  6. The absolute path of the file.
  7. Warning
  8. We only add the ResourcePath before the relative path of the file.
  9. Deprecated:
  10. Please use fullPathForFilename instead.
复制代码
  1. //
  2. //  TDInvFileUtils.h
  3. //  MyCocoa2DTest
  4. //
  5. //  Created by 韦 柱全 on 13-2-27.
  6. //
  7. //

  8. #ifndef __MyCocoa2DTest__TDInvFileUtils__
  9. #define __MyCocoa2DTest__TDInvFileUtils__

  10. #include <iostream>
  11. #include "cocos2d.h"
  12. using namespace cocos2d;
  13. using namespace std;

  14. /** 负责操作文件储存和读取
  15. */

  16. class CCReadFile {
  17. public:
  18.     /** 读取本地文件,返回数据 */
  19.     static string getFileByName(string pFileName);
  20.     
  21.     /** 储存内容到文件 */
  22.     static bool saveFile(char* pContent,string pFileName);
  23.     
  24. };

  25. #endif /* defined(__MyCocoa2DTest__TDInvFileUtils__) */
复制代码
  1. //
  2. //  TDInvFileUtils.cpp
  3. //  MyCocoa2DTest
  4. //
  5. //  Created by 韦 柱全 on 13-2-27.
  6. //
  7. //

  8. #include "CCReadFile.h"

  9. string CCReadFile::getFileByName(string pFileName){


  10.         //string path = CCFileUtils::sharedFileUtils()->getWriteablePath()+pFileName ;
  11.         string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pFileName.c_str());
  12.         CCLOG("path = %s",path.c_str());

  13.         FILE* file = fopen(path.c_str(), "r");

  14.         if (file) {
  15.                 char* buf;   
  16.                 int len;    
  17.                 /*获取长度*/
  18.                 fseek(file, 0, SEEK_END);   //移到尾部
  19.                 len = ftell(file);          //提取长度
  20.                 rewind(file);               //回归原位
  21.                 CCLOG("count the file content len = %d",len);
  22.                 //分配buf空间
  23.                 buf = (char*)malloc(sizeof(char) * len + 1);
  24.                 if (!buf) {
  25.                         CCLOG("malloc space is not enough.");
  26.                         return NULL;
  27.                 }

  28.                 //读取文件
  29.                 //读取进的buf,单位大小,长度,文件指针
  30.                 int rLen = fread(buf, sizeof(char), len, file);
  31.                 buf[rLen] = '\0';
  32.                 CCLOG("has read Length = %d",rLen);
  33.                 CCLOG("has read content = %s",buf);

  34.                 string result = buf;
  35.                 fclose(file);
  36.                 free(buf);
  37.                 return result;
  38.         }
  39.         else
  40.                 CCLOG("open file error.");

  41.         return NULL;
  42. }

  43. bool CCReadFile::saveFile(char *pContent, string pFileName){
  44.         //第一获取储存的文件路径
  45.         string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + pFileName;
  46.         CCLOG("wanna save file path = %s",path.c_str());

  47.         //创建一个文件指针
  48.         //路径、模式
  49.         FILE* file = fopen(path.c_str(), "w");
  50.         if (file) {
  51.                 fputs(pContent, file);
  52.                 fclose(file);
  53.         }
  54.         else
  55.                 CCLOG("save file error.");

  56.         return false;
  57. }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值