cocos2d-x的文件操作,对于一个新手来说,着实是不容易(其实我是新手...乃们看粗来了么),这里我写了几个简单的,有需要的可以拿去用,觉得好用顶一下,不费电哟~
头文件:
/*
author:pretty_man
time :2014.7.7
*/
#ifndef __FILE_OPTION_H__
#define __FILE_OPTION_H__
#include "cocos2d.h"
#include <stdio.h>
#include <vector>
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#endif
using namespace cocos2d;
using namespace std;
class FileOption
{
public:
/** 读取本地文件,返回数据 */
static string getFileByName(string pFileName);
/** 储存内容到文件 */
static bool saveFile(char* pContent,string pFileName);
/**删除文件,返回成功失败**/
static bool deleteFile(string pFileName);
/**创建文件,返回成功失败**/
static bool createFile(string pFileName);
/**创建文件夹**/
static bool createDirectory(const char *path);
/**判断文件是否存在**/
static bool FileOption::isFileExist(string pFileName );
};
#endif // __HELLOWORLD_SCENE_H__
.cpp
#include "FileOption.h"
/** 读取本地文件,返回数据 */
string FileOption::getFileByName(string pFileName){
//第一先获取文件的路径
string path = CCFileUtils::sharedFileUtils()->getWritablePath() + pFileName;
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 FileOption::saveFile(char* pContent,string pFileName){
//第一获取储存的文件路径
bool retValue =false;
string path = CCFileUtils::sharedFileUtils()->getWritablePath() + pFileName;
CCLOG("wanna save file path = %s",path.c_str());
//创建一个文件指针
//路径、模式
FILE* file = fopen(path.c_str(), "w");
if (file) {
fputs(pContent, file);
fclose(file);
retValue=true;
}
else
CCLOG("save file error.");
return retValue;
};
/**创建文件,返回成功失败**/
bool FileOption::createFile(string pFileName){
FILE *fp = fopen(pFileName.c_str(), "w");
bool bRet = false;
if (fp)
{
bRet = true;
fclose(fp);
}
return bRet;
};
/**创建文件夹,返回成功失败**/
bool FileOption::createDirectory(const char *path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
mode_t processMask = umask(0);
int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
umask(processMask);
if (ret != 0 && (errno != EEXIST))
{
return false;
}
return true;
#else
BOOL ret = CreateDirectoryA(path, NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
return true;
#endif
}
bool FileOption::isFileExist(string pFileName )
{
FILE *fp = fopen(pFileName.c_str(), "r");
bool bRet = false;
if (fp)
{
bRet = true;
fclose(fp);
}else{
CCLOG("file is not exist");
}
return bRet;
}
bool FileOption::deleteFile(string pFileName){
bool retValue =false;
string path = CCFileUtils::sharedFileUtils()->getWritablePath() + pFileName;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
if (isFileExist(pFileName))
{
rmdir(path.c_str());
retValue=true;
}
#else
if (isFileExist(pFileName))
{
remove(pFileName.c_str());
retValue=true;
}
#endif
return retValue;
};
运行结果:
github:https://github.com/wuyihua/FileOption/