援引http://blog.csdn.net/zhanghefu/article/details/21284323
http://blog.csdn.net/cxf7394373/article/details/7195661
先看一下用法别忘了引入头文件。 这里我是得到 UserDatas 文件夹下的全部 .json 文件的名字 并转成UTF-8
#include "MyFileUtils.h"#include "IconvString.h"MyFileUtils fileUtilMyFileUtils();vector fileNames fileUtil.getUserDataFilesName();vector fileNamesConverted;for (int j0;j
#ifndef __MYFILEUTILS_H__#define __MYFILEUTILS_H__#include "cocos2d.h"#include "io.h"#include "iostream"#include "vector"USING_NS_CC;using namespace std;class MyFileUtils{public:vector getUserDataFilesName(){//我自己的封装string strCCFileUtils::sharedFileUtils()->fullPathForFilename("UserDatas");getJsonFilesName(str.c_str(),files);return files;}void getFiles( string path, vector 0;//文件信息struct _finddata_t fileinfo;string p;if((hFile _findfirst(p.assign(path).append("\\*").c_str(), -1){do{//假设是文件夹,迭代之//假设不是,增加列表if((fileinfo.attrib 0 0)getFiles( p.assign(path).append("\\").append(fileinfo.name), files );}else{files.push_back(p.assign(path).append("\\").append(fileinfo.name) );}}while(_findnext(hFile, 0);_findclose(hFile);}}void getJsonFilesName( string path, vector 0;struct _finddata_t fileinfo;string p;if((hFile _findfirst(p.assign(path).append("\\*").c_str(), -1){do{string strfileinfo.name;if (str.find(".json")!-1){files.push_back( fileinfo.name ) ;}}while(_findnext(hFile, 0);_findclose(hFile);}}protected:private:vector files;};#endif
IconvString.h 转码工具类 功能转码解决中文乱码。使用iconv 库 注意头文件的引入每一个人可能不一样#ifndef ICONV_STRING_H#define ICONV_STRING_H#include #include "iconv/iconv.h"//通用代码int code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen);std::string utf2gb(const char *inbuf);std::string gb2utf(const char *inbuf);#endif
//IconvString.cpp 实现
#include "IconvString.h"int code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen){iconv_t cd;const char *temp inbuf;const char **pin char **pout memset(outbuf,0,outlen);cd iconv_open(to_charset,from_charset);if(cd0) return -1;if(iconv(cd,pin,-1) return -1;iconv_close(cd);return 0;}/*UTF8转为GB2312*/std::string utf2gb(const char *inbuf){size_t inlen strlen(inbuf);char * outbuf new char[inlen * 2 2];std::string strRet;if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 2) 0){strRet outbuf;}delete [] outbuf;return strRet;}/*GB2312转为UTF8*/std::string gb2utf(const char *inbuf){size_t inlen strlen(inbuf);char * outbuf new char[inlen * 2 2];std::string strRet;if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * 2 2) 0){strRet outbuf;}delete [] outbuf;return strRet;}
【感谢本站数字底座由龙石数据提供 http://www.longshidata.com/pages/government.html】