cocos2d-x CSV文件读取 (Excel生成csv文件)
2013-11-09 14:38
495 查看
一、准备素材
1.EXCEL表:内容如下2.EXCEL表转换为csv文件:方法很多,网上搜索就有。
[b] [/b]但要注意:文件保存为UTF-8格式的(否则中文显示乱码)
二、实现代码:
1.操作csv方法类为CCSVParseCCSVParse.h
#ifndef __cocos2d_x_Excel__CCSVParse__ #define __cocos2d_x_Excel__CCSVParse__ #include <iostream> #include "cocos2d.h" #include <vector> using namespace std; USING_NS_CC; class CCSVParse { public: // CCSVParse(void); ~CCSVParse(void); CCSVParse(istream& fin=cin, string sep=","): fieldsep(sep), cols(0) { } //用以存储数据 std::vector<std::vector<std::string> > data; bool openFile(const char* fileName); const char* getData(unsigned int rows, unsigned int cols); int findColsData(int cols, const char* value); inline int getCols(){return cols;} inline int getRows(){return data.size();} private: string fieldsep; int cols; void StringSplit(const string& str, vector<string>& tokens, const char& delimiters); void split(vector<string>& field, string line); int advplain(const string& line, string& fld, int); int advquoted(const string& line, string& fld, int); }; #endif /* defined(__cocos2d_x_Excel__CCSVParse__) */
CCSVParse.cpp
#include "CCSVParse.h" //CCSVParse::CCSVParse(void) //{ // //} CCSVParse::~CCSVParse(void) { } void CCSVParse::StringSplit( const string& str, vector<string>& tokens, const char& delimiters ) { string::size_type lastPos = str.find_first_not_of(delimiters, 0); string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != lastPos) { tokens.push_back(str.substr(lastPos, pos-lastPos)); lastPos = str.find_first_not_of(delimiters, pos); pos = str.find_first_of(delimiters, lastPos); } } void CCSVParse::split( vector<string>& field, string line ) { string fld; unsigned int i,j=0; if( line.length() ==0 ) return; i=0; do { if(j<line.length() && line[i]=='"') j = advquoted(line, fld, ++i); else j = advplain(line, fld, i); field.push_back(fld); i = j+1; } while (j<line.length()); } int CCSVParse::advplain( const string& s, string& fld, int i) { unsigned int j; j = s.find_first_of(fieldsep, i); if(j>s.length()) j=s.length(); fld = string(s,i,j-i); return j; } int CCSVParse::advquoted( const string& s, string& fld, int i) { unsigned int j; fld = ""; for (j=i; j<s.length(); ++j) { if(s[j]=='"' && s[++j]!='"') { unsigned int k = s.find_first_of(fieldsep, j); if(k>s.length()) k = s.length(); for(k-=j; k-->0;) fld += s[j++]; break; } fld += s[j]; } return j; } //解析 CVS 文件 bool CCSVParse::openFile( const char* fileName ) { string pathKey = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(fileName); unsigned char* pBuffer = NULL; unsigned long bufferSize = 0; pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "r", &bufferSize); string s = (char*)pBuffer; string str = s.substr(0,bufferSize); vector<string> line; StringSplit(str, line, '\n'); for(unsigned int i=0; i<line.size(); ++i) { vector<string> field; split(field, line[i]); data.push_back(field); cols = max(cols, (int)field.size()); } return true; } //获取指定行列的数据 const char* CCSVParse::getData(unsigned int rows, unsigned int cols ) { if (rows<0 || rows>=data.size() || cols<0 || cols>=data[rows].size()) { return ""; } return data[rows][cols].c_str(); } //获取指定数据的列下标 int CCSVParse::findColsData( int cols, const char* value ) { for (unsigned int i=0; i<data.size(); ++i) { if(strcmp(getData(i,cols),value)==0) return i; } return -1; }
在init内添加如下代码:
csvFile->openFile("testExcel.csv"); for (int i=0; i<csvFile->getCols(); ++i) { string strLine = ""; for(int j=0; j<csvFile->getRows(); ++j) { strLine += csvFile->getData(i,j); strLine += ","; } CCLabelTTF* pLab = CCLabelTTF::create(strLine.c_str(),"Arial",20); pLab->setColor(ccc3(255, 0, 0)); pLab->setPosition(ccp(size.width/2,size.height-150-i*30)); this->addChild(pLab,2); } delete csvFile;
运行效果如下:
引用博文:http://www.cnblogs.com/MrGreen/archive/2013/09/03/3295662.html
相关文章推荐
- 关于指针的一些事情
- C/C++数据对齐详细解析
- C++中引用的使用总结
- C++中引用(&)的用法与应用实例分析
- 解析C++ 浮点数的格式化输出
- 深入分析C++中几个最不常用的关键字
- c++中inline的用法分析
- 深入解析C++ Data Member内存布局
- 从汇编看c++中默认构造函数的使用分析
- 关于C++中的友元函数的一些总结
- C++的sstream标准库详细介绍
- 基于C++自动化编译工具的使用详解
- 浅谈C++中的string 类型占几个字节
- C/C++ 宏详细解析
- 深入分析C++中两个大数相乘结果不正确的问题
- 探讨C++中数组名与指针的用法比较分析
- 深入解析C++中的引用类型
- C++可变参数的实现方法
- C++中的常对象与常对象成员详解
- 基于C++类型重定义的使用详解