Cocos2d C++ 解析CSV
来源:互联网 发布:冒险岛网络传输异常 编辑:程序博客网 时间:2024/06/07 10:34
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 -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
1 0
- Cocos2d C++ 解析CSV
- Cocos2d C++ 解析CSV
- Cocos2d-x C++解析CSV
- C语言解析CSV文件!
- C语言解析csv文件
- Cocos2d-x数据篇05:CSV文件解析
- 解析csv格式文件的一段C代码
- CSV解析
- csv解析
- CSV解析
- 解析csv
- [寒江孤叶丶的Cocos2d-x之旅_24]Cocos2d-x LUA 解析 Csv文件的方法
- CHCSVParser, 用于 Objective C的合适的CSV解析器
- cocos2d-x解析json文件(C++)
- CSV文件格式解析
- Java 解析csv文件
- CSV文件解析
- csv解析方案(Java)
- 已解决:sql server 删除用户时报错:(存在该用户的组织构架,无法删除)
- input_report APIs
- Android--showDialog()、onPrepareDialog()和onCreateDialog()的关系 .
- iOS 的 XMPPFramework 简介
- 如何交换a,b的值。
- Cocos2d C++ 解析CSV
- 如何在C/C++中动态分配二维数组
- 小米4.4.4 miui 调用系统裁剪没有返回数据
- test1
- android ImageButton和Button区别
- jQuery命名库冲突的问题
- onvif开发实战1--2015年5月20日总结框架搭建
- linux中shell变量$#,$@,$0,$1,$2的含义解释:
- V4L2 所支持的视频图像格式