Cocos2d-x中的文件读取操作

来源:互联网 发布:手机如何联系淘宝小二 编辑:程序博客网 时间:2024/05/01 01:29

转载自:http://blog.csdn.net/chiuan/article/details/8618411

为了保存自定义数据文件,需要保存文件和读取文件,也就是File的IO处理;

针对cocos2d-x我们可以通过CCFileUtils::sharedFileUtils()->getWriteablePath()获取到可读写的文件目录,其实是Caches目录。


关于file的操作,我们要明白几个概念:

File :文件对象,用于创建文件,操作文件

fopen:打开操作一个具体文件(文件路径,模式)模式有"w"\"r"读写等

fseek:移动文件指针

ftell:得到文件指针的位置,距离开头

rewind:文件指针重置

malloc:分配内存空间

fread:读一个文件的内容,需要输入buf储存空间,单位大小,长度,文件指针

fputs:写内容进去一个文件


摘录读取模式

    r 以只读方式打开文件,该文件必须存在。   
    r+ 以可读写方式打开文件,该文件必须存在。   
    rb+ 读写打开一个二进制文件,允许读数据。   
    rt+ 读写打开一个文本文件,允许读和写。   
    w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。   
    w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。   
    a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保    留。(EOF符保留)   
    a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留)   
    wb 只写打开或新建一个二进制文件;只允许写数据。   
    wb+ 读写打开或建立一个二进制文件,允许读和写。   
    wt+ 读写打开或着建立一个文本文件;允许读写。   
    at+ 读写打开一个文本文件,允许读或在文本末追加数据。   
    ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。


以下是代码,2个静态方法,保存和读取:TDInvFileUtils.h

[cpp] view plaincopyprint?
  1. //  
  2. //  TDInvFileUtils.h  
  3. //  MyCocoa2DTest  
  4. //  
  5. //  Created by 韦 柱全 on 13-2-27.  
  6. //  
  7. //  
  8.   
  9. #ifndef __MyCocoa2DTest__TDInvFileUtils__  
  10. #define __MyCocoa2DTest__TDInvFileUtils__  
  11.   
  12. #include <iostream>  
  13. #include "cocos2d.h"  
  14. using namespace cocos2d;  
  15. using namespace std;  
  16.   
  17. /** 负责操作文件储存和读取 
  18.  */  
  19.   
  20. class TDInvFileUtils {  
  21. public:  
  22.     /** 读取本地文件,返回数据 */  
  23.     static string getFileByName(string pFileName);  
  24.       
  25.     /** 储存内容到文件 */  
  26.     static bool saveFile(char* pContent,string pFileName);  
  27.       
  28. };  
  29.   
  30. #endif /* defined(__MyCocoa2DTest__TDInvFileUtils__) */  

其实现文件 TDInvFileUtils.cpp

[cpp] view plaincopyprint?
  1. //  
  2. //  TDInvFileUtils.cpp  
  3. //  MyCocoa2DTest  
  4. //  
  5. //  Created by 韦 柱全 on 13-2-27.  
  6. //  
  7. //  
  8.   
  9. #include "TDInvFileUtils.h"  
  10.   
  11. string TDInvFileUtils::getFileByName(string pFileName){  
  12.     //第一先获取文件的路径  
  13.     string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + pFileName;  
  14.     CCLOG("path = %s",path.c_str());  
  15.       
  16.     //创建一个文件指针  
  17.     FILE* file = fopen(path.c_str(), "r");  
  18.       
  19.     if (file) {  
  20.         char* buf;  //要获取的字符串  
  21.         int len;    //获取的长度  
  22.         /*获取长度*/  
  23.         fseek(file, 0, SEEK_END);   //移到尾部  
  24.         len = ftell(file);          //提取长度  
  25.         rewind(file);               //回归原位  
  26.         CCLOG("count the file content len = %d",len);  
  27.         //分配buf空间  
  28.         buf = (char*)malloc(sizeof(char) * len + 1);  
  29.         if (!buf) {  
  30.             CCLOG("malloc space is not enough.");  
  31.             return NULL;  
  32.         }  
  33.           
  34.         //读取文件  
  35.         //读取进的buf,单位大小,长度,文件指针  
  36.         int rLen = fread(buf, sizeof(char), len, file);  
  37.         buf[rLen] = '\0';  
  38.         CCLOG("has read Length = %d",rLen);  
  39.         CCLOG("has read content = %s",buf);  
  40.           
  41.         string result = buf;  
  42.         fclose(file);  
  43.         free(buf);  
  44.         return result;  
  45.     }  
  46.     else  
  47.         CCLOG("open file error.");  
  48.       
  49.     return NULL;  
  50. }  
  51.   
  52. bool TDInvFileUtils::saveFile(char *pContent, string pFileName){  
  53.     //第一获取储存的文件路径  
  54.     string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + pFileName;  
  55.     CCLOG("wanna save file path = %s",path.c_str());  
  56.       
  57.     //创建一个文件指针  
  58.     //路径、模式  
  59.     FILE* file = fopen(path.c_str(), "w");  
  60.     if (file) {  
  61.         fputs(pContent, file);  
  62.         fclose(file);  
  63.     }  
  64.     else  
  65.         CCLOG("save file error.");  
  66.       
  67.     return false;  
0 0