大佬教程收集整理的这篇文章主要介绍了cocos2d-x 读写xml文件,沙盒路径下超详细攻略(object向c++转型系列教程5),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
第一:读取沙盒下xml文件
1,先取得访问路劲:
std::String documentPath = CCFileUtils::getWriteablePath();
String filename = "filename.xml";
String filePath = documentPath + filename;
String path = filePath;
FILE *fp =fopen(path.c_str(),"r");//根据路径打开文件
char *pchBuf = NULL;//将要取得的字符串
int nLen = 0;//将要取得的字符串长度
fseek(fp,0,SEEK_END);//文件指针移到文件尾
nLen =ftell(fp); //得到当前指针位置,即是文件的长度
//动态申请空间,为保存字符串结尾标志\0,多申请一个字符的空间
pchBuf = (char*)@H_430_124@malloc(sizeof(char)*nLen+1);
if(!pchBuf)
perror("内存不够!\n");
exit(0);
}
//读取文件内容//读取的长度和源文件长度有可能有出入,这里自动调整 nLen
nLen =fread(pchBuf,sizeof(char),nLen,fp);
pchBuf[nLen] ='\0';//添加字符串结尾标志
printf("%s\n",pchBuf);//把读取的内容输出到屏幕看看
String detailStr = pchBuf;
free(pchBuf);//释放空间
第二:写入xml文件到沙盒下
要保存的xml
String xmlStr = "<xml>MyXml<xml>";
constchar * aChar = xmlStr.c_str();
取得文件储存的位置
String documentPath = getWriteablePath();
String filename = "filename.xml";
String filePath = documentPath + filename;
// 保存字符串为xml文件
String path = filePath;
FILE *fp =fopen(path.c_str(),"w");
fputs(aChar,fp);
fclose(fp);
以上是大佬教程为你收集整理的cocos2d-x 读写xml文件,沙盒路径下超详细攻略(object向c++转型系列教程5)全部内容,希望文章能够帮你解决cocos2d-x 读写xml文件,沙盒路径下超详细攻略(object向c++转型系列教程5)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。