ID,主题关卡名字,主题背景音乐,主题背景图片,1,关卡名字1,test.mp3,test.png,2,关卡名字2,3,关卡名字3,
就是以英文‘,’作为分隔符的文件。这种结构有点像数据库表的结构,因为非常简单,所以适用范围比较广,Excel可以导出CSV,sqlite 等数据库也可以导出CSV。
在游戏开发中,这个文件一般给策划来进行编辑,修改数值后很容易测试,不需要编译游戏。我个人是比较喜欢脚本开发,让策划直接修改脚本,省去CSV这一部。
2.解析CSV
网络上有很多版本,写的都太复杂了。有些还不能很好工作。我觉得好的版本是只依赖C++ 的stl的,而且返回的值应该是一个二维的字符串数组vector<vector<string> >。我写的如下:
CSVParser.h
#ifndef TestConfig_CppCSV_h#define TestConfig_CppCSV_h#include <fstream>#include <string>#include <iostream>#include <vector>using namespace std;class CSVParser{public: CSVParser(const char* filename); ~CSVParser(); vector<vector<string> > data; static string Trimstring(string& str);};#endif
CSVParser.cpp
#include "CSVParser.h"CSVParser::CSVParser(const char* filename){ data.clear(); std::ifstream file(filename); std::string line; //get each line string while (getline(file,line)) { istringstream sin(line); vector<string> fIElds; string fIEld; while (getline(sin,fIEld,',')) { fIElds.push_back(CSVParser::Trimstring(fIEld)); } data.push_back(fIElds); } file.close();}CSVParser::~CSVParser(){ data.clear();}string CSVParser::Trimstring(string& str){ //replace \r string::size_type i = 0,j = 0; j = str.find_first_of("\n\r",i); if (j < str.size()){ str.erase(j,1); } j = str.find_first_of("\r",1); } return str;}
使用(cocos2d -x 3.x 版本):
CSVParser parser = CSVParser(fileUtils::getInstance()->fullPathForfilename("test.csv").c_str()); vector<vector<string> > data = parser.data;
http://www.waitingfy.com/archives/1722
总结以上是内存溢出为你收集整理的Cocos2d C++ 解析CSV全部内容,希望文章能够帮你解决Cocos2d C++ 解析CSV所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)