转载: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

  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 TDInvFileUtils {
  17. public:
  18. /** 读取本地文件,返回数据 */
  19. static string getFileByName(string pFileName);
  20. /** 储存内容到文件 */
  21. static bool saveFile(char* pContent,string pFileName);
  22. };
  23. #endif /* defined(__MyCocoa2DTest__TDInvFileUtils__) */

其实现文件 TDInvFileUtils.cpp

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

最新文章

  1. 【Reading Note】Python读书杂记
  2. Python正则式的基本用法
  3. 802.11MAC基础
  4. Xperf Analysis Basics(转)
  5. 【JavaScript学习笔记】if使用
  6. [GeekBand] STL与泛型编程(3)
  7. SpringMVC4+MyBatis+SQL Server2014+druid 监控SQL运行情况
  8. EL表达式隐式对象
  9. hibernate的一级和二级缓存
  10. Gradle打jar包命令
  11. HDU 2196.Computer 树形dp 树的直径
  12. 路由网关---zuul
  13. thymeleaf-extras-shiro
  14. eclipse 访问 hive1.2.1
  15. UNIX环境高级编程 第10章 信号
  16. CORS跨域请求C#版
  17. 网络安全-使用HTTP动词篡改的认证旁路
  18. SugarCE问题点记录
  19. F - Goldbach`s Conjecture 对一个大于2的偶数n,找有多少种方法使两个素数的和为n;保证素数a&lt;=b; a+b==n; a,b都为素数。
  20. easy_install

热门文章

  1. SQL效率问题
  2. javascript中===与==
  3. linux内核链表分析
  4. Zookeeper、HBase的伪分布
  5. View的个得区域函数getHitRect,getDrawingRect,getLocalVisibleRect,getGlobalVisibleRect(*)
  6. shell/bash 让vi/vim显示空格,及tab字符
  7. openVPN使用
  8. Save output to a text file from Mac terminal
  9. 函数buf_read_page
  10. HDU 1280 前m大的数【哈希入门】