参照了别人的程序,修改了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
-
pszRelativePath The 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;
- }