展会信息港展会大全

Cocos2d C++ 解析CSV,cocos2d解析csv
来源:互联网   发布日期:2015-09-28 11:21:44   浏览:1948次  

导读: Cocos2d C++ 解析CSV,cocos2d解析csv 1.什么是CSV Id,主题关卡名字,主题背景音乐,主题背景图片, 1,关卡名字1,test.mp3,test.png, 2,关...

Cocos2d C++ 解析CSV,cocos2d解析csv

1.什么是CSV

Id,主题关卡名字,主题背景音乐,主题背景图片,

1,关卡名字1,test.mp3,test.png,

2,关卡名字2,test.mp3,test.png,

3,关卡名字3,test.mp3,test.png,

就是以英文‘,’作为分隔符的文件。这种结构有点像数据库表的结构,因为非常简单,所以适用范围比较广,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", i);

if (j < str.size()){

str.erase(j, 1);

}

return str;

}

使用(cocos2d -x3.x 版本):

CSVParser parser = CSVParser(FileUtils::getInstance()->fullPathForFilename("test.csv").c_str());

vector<vector<string> > data = parser.data;

http://www.waitingfy.com/archives/1722

http://www.bkjia.com/Androidjc/1003632.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/1003632.htmlTechArticleCocos2d C++ 解析CSV,cocos2d解析csv 1.什么是CSV Id,主题关卡名字,主题背景音乐,主题背景图片,1,关卡名字1,test.mp3,test.png,2,关卡名字2,test.mp3,test....

赞助本站

人工智能实验室

相关热词: android开发 android教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港