Cocos2d-x读取本地文件
来源:互联网 发布:概率图模型 知乎 编辑:程序博客网 时间:2024/05/17 09:05
在公司在项目开发的时候需要读取本地的文件,于是在网上搜索了一下以下关于Cocos2d-x文件读取的操作,用了两种方法都可以实现,一种是使用C++另种是Cocos2d-x代码如下:
//读取文件(参数分别为文件名和文本框)
void GameRegistry::readFile(const char *pFileName,UILabel *pLabel){
/*方法一
ifstream inFile;inFile.open(pFileName);//打开文件
string pSaveStr;//用于保存读取一行的文件内容
for (string str;getline(inFile,str);)//一行行的读取
{
GBKToUTF8(str,"gbk","utf-8"); //中文转码,这个也是借鉴网上的
pSaveStr += str + "\n";}
//设置文本框内容
pLabel->setText(pSaveStr);pLabel->setFontName("微软雅黑");
//关闭文件
inFile.close();
*/
//方法二
//获取文件在系统的绝对路径string filePath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pFileName);
//读取的内容
unsigned char *data = NULL;
//读取的字节数,读取失败则为0
unsigned long len = 0;
data = CCFileUtils::sharedFileUtils()->getFileData(filePath.c_str(),"r",&len);
//unsigned char* 转 sting
std::string my_std_string(reinterpret_cast<const char *>(data),len);
//中文转码
GBKToUTF8(my_std_string,"gbk","utf-8");
pLabel->setText(my_std_string);
pLabel->setFontName("微软雅黑");
//释放内存
if (len>0&&data)
{
delete[] data;
}
}
0 0
- Cocos2d-x读取本地文件
- cocos2d-x 读取 文件
- Cocos2d-x 读取PVR
- cocos2d-x进度条读取
- cocos2d-x 读取文件
- cocos2d-x读取json
- cocos2d-x jsonCpp 读取json
- cocos2d-x 读取plist文件
- cocos2d-x plist文件读取
- cocos2d-x 读取文本文件(txt)
- cocos2d-x 文本读取CCFileUtils
- cocos2d-x 读取配置文件plist
- cocos2d-x 读取.plist文件
- cocos2d-x -----文件的读取
- 读取本地文件
- 读取本地文件
- 读取本地文件
- 读取json 读取本地文件
- 命令别名与历史命令
- java实现单链表的基本操作
- Eclipse无法自动补全代码的解决办法
- Atlantis(扫描线+线段树+离散化)
- Java中的内存模型和多线程分析
- Cocos2d-x读取本地文件
- UVa1218 Perfect Service
- 关于正则表达式
- The Balance(扩展欧几里得算法)
- 每天一点android开发---------ContentProvider
- mean shift 图像分割(二)
- Linux/Ubuntu下 静态编译Qt程序
- Android中Notification的使用
- 有时候不要让自己太累 找个途径就可以解决