cocos2d-x文件操作(cocos2d-x 2.2)

本文介绍了一个用于Cocos2d-x的游戏文件操作类,包括读取、写入、创建、删除文件及文件夹等功能,并提供了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       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/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值