cocos2d-x:读取指定文件夹下的文件名称+解决中文乱码(win32下有效)

时间:2021-01-28 14:01:16

援引: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 fileUtil=MyFileUtils();
vector<string> fileNames= fileUtil.getUserDataFilesName();	
vector<string> fileNamesConverted;
 for (int j=0;j<fileNames.size();j++){ 
  fileNamesConverted.push_back(      gb2utf(  fileNames[j].c_str()  )     );
 }


MyFileUtils.h 文件,直接在.h文件里实现  功能:读取指定文件夹下的文件名称。

#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<string> getUserDataFilesName(){//我自己的封装

	string str=CCFileUtils::sharedFileUtils()->fullPathForFilename("UserDatas");
	getJsonFilesName(str.c_str(),files);

	return files;

}
void getFiles( string path, vector<string>& files ) //通用代码
{
	//文件句柄
	long   hFile   =   0;
	//文件信息
	struct _finddata_t fileinfo;
	string p;
	if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
	{
		do
		{
			//假设是文件夹,迭代之
			//假设不是,增加列表
			if((fileinfo.attrib &  _A_SUBDIR))
			{
				if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)
					getFiles( p.assign(path).append("\\").append(fileinfo.name), files );
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
			}
		}while(_findnext(hFile, &fileinfo)  == 0);
		_findclose(hFile);
	}
}

void getJsonFilesName( string path, vector<string>& files )//我自己的封装
{
	long   hFile   =   0;
	struct _finddata_t fileinfo;
	string p;
	if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) !=  -1)
	{
		do
		{			
			string str=fileinfo.name;
			if (str.find(".json")!=-1)
			{
				files.push_back( fileinfo.name ) ;
			}

		}while(_findnext(hFile, &fileinfo)  == 0);
		_findclose(hFile);
	}
}

protected:

private:
	vector<string> files;
	

};
#endif




IconvString.h 转码工具类   功能:转码,解决中文乱码。使用iconv 库
注意头文件的引入,每一个人可能不一样

#ifndef ICONV_STRING_H
#define ICONV_STRING_H
#include <string>
#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
</pre><pre code_snippet_id="316364" snippet_file_name="blog_20140428_6_4985379" name="code" class="cpp">//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 = &temp;
	char **pout = &outbuf;
	memset(outbuf,0,outlen);
	cd = iconv_open(to_charset,from_charset);
	if(cd==0) return -1;
	if(iconv(cd,pin,&inlen,pout,&outlen)==-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;
}