[cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类
来源:互联网 发布:java中random怎么用 编辑:程序博客网 时间:2024/05/21 17:34
一直想学习下游戏编程,但总是没下定决心。现在就从Cocos2dx开始学习吧,以后也要坚持写些经验文章,就当是给我自己的学习历程的一个记录吧。
我现在下的cocos2dx版本是3.0beta2的,而网上的大多数教程都是2.x的,有些地方有些小不同,所以难免碰到点磕碰。但这些总是难免的,一下子就习惯了。
3.0没有了CC前缀,这样看起来果真是爽多了啊
cocos2dx的文件工具类 FileUtils,用于对文件的简单操作,下面列出一些大概用得到的函数:
static FileUtils* getInstance();//获取FileUtils类的实例
cocos2dx中有很多拥有共享实例的类,再以前版本的获取实例是用sharedXXX()函数来获取的,而3.0都只使用一个函数getInstance()来获取。可以看下面的说明:/** @deprecated Use getInstance() instead */ CC_DEPRECATED_ATTRIBUTE static FileUtils* sharedFileUtils() { return getInstance(); }
可以看出虽然现在还可以使用sharedFileUtils(),但他已经被标注为废弃。很显然使用getInstance()更加明了简介。
std::string getStringFromFile(const std::string& filename);//读取文件中的字符串
Data getDataFromFile(const std::string& filename);//获取文件数据
下面看一下Data类
class CC_DLL Data{public: static const Data Null; //构造函数 Data(); Data(const Data& other); Data(Data&& other); ~Data(); // 重载符号 Data& operator= (const Data& other); Data& operator= (Data&& other); unsigned char* getBytes() const;//获取数据 ssize_t getSize() const;//尺寸 void copy(unsigned char* bytes, const ssize_t size);//从bytes复制 void fastSet(unsigned char* bytes, const ssize_t size);//从bytes快速set,使用后bytes将不能在外部使用 void clear();//清除 bool isNull() const;//判空private: void move(Data& other);private: unsigned char* _bytes; ssize_t _size;};
unsigned char* getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size);//读取压缩文件数据(zip格式)
如果读取成功size中会返回文件的大小,否则返回0。
std::string fullPathForFilename(const std::string &filename);//获取文件的完整路径
如果我们通过setSearchPaths()设置搜索路径("/mnt/sdcard/", "internal_dir/"),然后通过setSearchResolutionsOrder()设置子区分路径("resources-ipadhd/", "resources-ipad/", "resources-iphonehd")。如果搜索文件名为'sprite.png' 那么会先在文件查找字典中查找key: sprite.png -> value: sprite.pvr.gz,然后搜索文件'sprite.pvr.gz'如下顺序:
/mnt/sdcard/resources-ipadhd/sprite.pvr.gz (if not found, search next) /mnt/sdcard/resources-ipad/sprite.pvr.gz (if not found, search next) /mnt/sdcard/resources-iphonehd/sprite.pvr.gz (if not found, search next) /mnt/sdcard/sprite.pvr.gz (if not found, search next) internal_dir/resources-ipadhd/sprite.pvr.gz (if not found, search next) internal_dir/resources-ipad/sprite.pvr.gz (if not found, search next) internal_dir/resources-iphonehd/sprite.pvr.gz (if not found, search next) internal_dir/sprite.pvr.gz (if not found, return "sprite.png")
如果找到返回完整路径,没找到返回'sprite.png'。
void loadFilenameLookupDictionaryFromFile(const std::string &filename);//从文件导入文件名查找字典
文件为plist格式如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>filenames</key> <dict> <key>sounds/click.wav</key> <string>sounds/click.caf</string> <key>sounds/endgame.wav</key> <string>sounds/endgame.caf</string> <key>sounds/gem-0.wav</key> <string>sounds/gem-0.caf</string> </dict> <key>metadata</key> <dict> <key>version</key> <integer>1</integer> </dict> </dict> </plist>
key对应string
void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);//从ValueMap中设置文件名查找字典
ValueMap的定义:
typedef std::unordered_map<std::string, Value> ValueMap;std::string fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile);//获取相对应文件的完整路径 e.g. filename: hello.png, pszRelativeFile: /User/path1/path2/hello.plist Return: /User/path1/path2/hello.pvr (If there a a key(hello.png)-value(hello.pvr) in FilenameLookup dictionary. )
void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);//设置子搜索区分路径
见fullPathForFilename()。
void addSearchResolutionsOrder(const std::string &order);//增加子搜索路径
const std::vector<std::string>& getSearchResolutionsOrder();//获取子搜索区分路径
void setSearchPaths(const std::vector<std::string>& searchPaths);//设置搜索路径
见fullPathForFilename()。
void addSearchPath(const std::string & path);//增加搜索路径
const std::vector<std::string>& getSearchPaths() const;//获取搜索路径
std::string getWritablePath();//获取一个可写入文件的路径
经过测试在win32平台上,debug版本返回的是程序文件所在的路径,release返回的是“我的文档”路径。
bool isFileExist(const std::string& filePath);//判断文件是否存在
经过测试在win32平台上,如果路径中包含中文字符会找不到文件。所以可以自己写个
bool wFileIO::isFileExist(const std::string& pFileName){#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)return CCFileUtils::getInstance()->isFileExist(pFileName);#elseif(GetFileAttributesA(pFileName.c_str()) == INVALID_FILE_ATTRIBUTES)return false;return true;#endif}
bool isAbsolutePath(const std::string& path);判断是否为绝对路径
void setPopupNotify(bool notify);
bool isPopupNotify();
Sets/Gets 当文件加载失败时弹出messagebox.
ValueMap getValueMapFromFile(const std::string& filename);//从文件获取ValueMap
bool writeToFile(ValueMap& dict, const std::string& fullPath);//写入一个ValueMap数据到plist格式文件
ValueVector getValueVectorFromFile(const std::string& filename);//从文件获取ValueVector
ValueVector定义:
typedef std::vector<Value> ValueVector;
std::string wFileIO::getWritablePath(){#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)return CCFileUtils::getInstance()->getWritablePath();#else char full_path[MAX_PATH + 1]; ::GetModuleFileNameA(NULL, full_path,MAX_PATH + 1);std::string ret((char*)full_path); // remove xxx.exe ret = ret.substr(0, ret.rfind("\\") + 1); ret = convertPathFormatToUnixStyle(ret); return ret;#endif}下面是保存文件:
bool wFileIO::saveFile(const char* pContentString, const std::string& pFileName){std::string fn=convertPathFormatToUnixStyle(pFileName);int np=fn.rfind('/');if(np!=std::string::npos)if(!mkDirM(fn.substr(0,np)))return false;std::string path = getWritablePath()+fn; FILE* file = fopen(path.c_str(), "w"); if (file){ fputs(pContentString, file); fclose(file); log("save file [%s]",path.c_str()); return true;}elselog("fail to save file [%s]!",path.c_str());return false;}//检测各级文件夹,不存在则创建bool wFileIO::mkDirM(const std::string& pDirName){std::string path = getWritablePath();int np=pDirName.find('/',0);while(np!=std::string::npos){if(!mkDir(path+pDirName.substr(0,np)))return false;np=pDirName.find('/',np+1);}return mkDir(path+pDirName);}//创建文件夹bool wFileIO::mkDir(const std::string& pDirName){#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)DIR *pDir = NULL;//打开该路径pDir = opendir (pDirName.c_str());if (! pDir){//创建该路径if(!mkdir(pDirName.c_str(), S_IRWXU | S_IRWXG | S_IRWXO)){log("fail to create dir [%s]",pDirName.c_str());return false;}log("create dir [%s]",pDirName.c_str());}#elseif ((GetFileAttributesA(pDirName.c_str())) == INVALID_FILE_ATTRIBUTES){if(!CreateDirectoryA(pDirName.c_str(), 0)){log("fail to create dir [%s]",pDirName.c_str());return false;}log("create dir [%s]",pDirName.c_str());}#endifreturn true;}//路径格式转为UnixStyle,"c:\xxx.txt" --> "c:/xxx.txt"static inline std::string convertPathFormatToUnixStyle(const std::string& path){ std::string ret = path; int len = ret.length();for (int i = 0; i < len; ++i) { if (ret[i] == '\\') { ret[i] = '/'; } } return ret;}好了,Cocos2dx中的文件操作就学习到这里了。
- [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类
- Cocos2dx基础 | [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类
- cocos2dx 3.0 对文件读写操作 +FileUtils类
- [cocos2dx 3.0] 对文件读写操作 +FileUtils类
- FileUtils 对文件操作
- cocos2dx[3.2]——文件操作FileUtils
- 对文件操作之FileUtils方法大全
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- 使用FILEUTILS读写文件
- Android 文件操作帮助类FileUtils文件
- 文件操作工具类 FileUtils常用方法
- FileUtils 基本文件操作
- FileUtils文件操作
- Coco2dx文件操作FileUtils
- java FileUtils快速读写文件
- pat 1045
- oracle中 rownum和rowid的用法
- Android中常用适配器及定义自己的适配器
- ANDROID性能调优
- BF算法和KMP算法对比
- [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类
- CF 229B. Planets
- 从Mysql到系统权限
- [linux+c]C语言题目(一)
- gcc: c-parse.c: No such file or directory
- POJ 2255 Tree Recovery 二叉树+遍历+递归
- JAVA DAO设计模式
- mysql基础---日志文件
- DB2数据库OLAP函数使用