Cocos2D-x文件读取I/O

cocos2d-x提供了getFileData接口,使用方法如下

char* file = (char*)CCFileUtils::sharedFileUtils()->getFileData("testjson.json","r", &size); 


其它方法如下:

参照了别人的程序,修改了Path:

	//string path = CCFileUtils::sharedFileUtils()->getWriteablePath()+pFileName ;
	string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pFileName.c_str());

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

const char* fullPathFromRelativePath(const char *  pszRelativePath) 

Generate the absolute path of the file.

Parameters
pszRelativePathThe relative path of the file.
Returns
The absolute path of the file.
Warning
We only add the ResourcePath before the relative path of the file.
Deprecated:
Please use fullPathForFilename instead.


//
//  TDInvFileUtils.h
//  MyCocoa2DTest
//
//  Created by 韦 柱全 on 13-2-27.
//
//

#ifndef __MyCocoa2DTest__TDInvFileUtils__
#define __MyCocoa2DTest__TDInvFileUtils__

#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;

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

class CCReadFile {
public:
    /** 读取本地文件,返回数据 */
    static string getFileByName(string pFileName);
    
    /** 储存内容到文件 */
    static bool saveFile(char* pContent,string pFileName);
    
};

#endif /* defined(__MyCocoa2DTest__TDInvFileUtils__) */



  

//
//  TDInvFileUtils.cpp
//  MyCocoa2DTest
//
//  Created by 韦 柱全 on 13-2-27.
//
//

#include "CCReadFile.h"

string CCReadFile::getFileByName(string pFileName){
 

	//string path = CCFileUtils::sharedFileUtils()->getWriteablePath()+pFileName ;
	string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(pFileName.c_str());
	CCLOG("path = %s",path.c_str());
 
	FILE* file = fopen(path.c_str(), "r");

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

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

		string result = buf;
		fclose(file);
		free(buf);
		return result;
	}
	else
		CCLOG("open file error.");

	return NULL;
}

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

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

	return false;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值