cocos2d-x CSV文件读取 (Excel生成csv文件)
实现类
CCSVParse.h
#ifndef __C_CSV_PARSE__
#define __C_CSV_PARSE__ #include "cocos2d.h"
#include <vector>
using namespace std; class CCSVParse
{
public:
//CCSVParse(void);
~CCSVParse(void); CCSVParse(istream& fin=cin, string sep=","):
fieldsep(sep),
cols()
{ } //用以存储数据
std::vector<std::vector<std::string>> data; 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); public:
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();};
}; #endif //__C_CSV_PARSE__
CCSVParse.cpp
#include "CSVParse.h" using namespace cocos2d; // 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, );
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=; if( line.length() == )
return;
i=; do
{
if(j<line.length() && line[i]=='"')
j = advquoted(line, fld, ++i);
else
j = advplain(line, fld, i); field.push_back(fld);
i = j+;
} 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-->;)
fld += s[j++];
break;
}
fld += s[j];
}
return j;
} //解析 CVS 文件
bool CCSVParse::openFile( const char* fileName )
{
string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
unsigned char* pBuffer = nullptr;
unsigned long bufferSize = ;
pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "r", &bufferSize); string s = (char*)pBuffer;
string str = s.substr(,bufferSize); vector<string> line;
StringSplit(str, line, '\n');
for(unsigned int i=; 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< || rows>=data.size() || cols< || cols>=data[rows].size())
{
return "";
}
return data[rows][cols].c_str();
} //获取指定数据的列下标
int CCSVParse::findColsData( int cols, const char* value )
{
for (unsigned int i=; i<data.size(); ++i)
{
if(strcmp(getData(i,cols),value)==)
return i;
}
return -;
}
HelloWorld.cpp 中 init() 函数中添加
CCSVParse* csvFile = new CCSVParse();
csvFile->openFile("Book1.csv");
for (int i=; i<csvFile->getCols(); ++i)
{
string strLine = "";
for(int j=; j<csvFile->getRows(); ++j)
{
strLine += csvFile->getData(i,j);
strLine += ",";
}
CCLabelTTF* pLab = CCLabelTTF::create(strLine.c_str(),"Arial",);
pLab->setPosition(ccp(size.width/,size.height--i*));
this->addChild(pLab,);
}
Book1.csv 内容 注意将文件保存为UTF-8格式的(否则中文显示乱码)
星期一,1,10000,HP1,MP1,数值1,Icon1.png
星期二,2,10001,HP2,MP2,数值2,Icon2.png
星期三,3,10002,HP3,MP3,数值3,Icon3.png
星期四,4,10003,HP4,MP4,数值4,Icon4.png
星期五,5,10004,HP5,MP5,数值5,Icon5.png
星期六,6,10005,HP6,MP6,数值6,Icon6.png
星期日,7,10006,HP7,MP7,数值7,Icon7.png
win32 平台 显示结果:(保存文件 非UTF-8格式)中文显示乱码

win32 平台 显示结果:(保存文件 为UTF-8格式) 正常显示

不早了,洗洗睡吧 , 明天又起不来了
cocos2d-x CSV文件读取 (Excel生成csv文件)的更多相关文章
- 使用OLEDB读取excel和csv文件
这是我第一次在博客上写东西,简单的为大家分享一个oledb读取文件的功能吧,这两天在做一个文件导入数据库的小demo,就想着导入前先在页面上展示一下,之前调用Microsoft.Office.Inte ...
- 用PHP读取Excel、CSV文件
PHP读取excel.csv文件的库有很多,但用的比较多的有: PHPOffice/PHPExcel.PHPOffice/PhpSpreadsheet,现在PHPExcel已经不再维护了,最新的一次提 ...
- 使用OLEDB方式 读取excel和csv文件
/// <summary> /// 使用OLEDB读取excel和csv文件 /// </summary> /// <param name="path" ...
- 通过oledb驱动读取excel、csv数据丢失解决方案
1.问题出现 在开发应用程序的过程中,比较常用一功能就是通过oledb驱动读取excel.csv.text等文件:而最近有客户反映,在使用短信平台(下载地址:http://www.sms1086.co ...
- 将CSV格式或者EXCEL格式的文件导入到HIVE数据仓库中
学习内容:数据导入,要求将CSV格式或者EXCEL格式的文件导入到HIVE数据仓库中: ①hive建表:test1 create table test1 (InvoiceNo String, Stoc ...
- java读取字符串,生成txt文件
/** * 读取字符串,生成txt 文件 已解决未设置编码时,在项目中直接打开文件,中文乱码问题 * WriteText.writeToText(musicInfo,fileName)直接调用 * * ...
- C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
报错信息: 最近打开下载的 Excel,会报如下错误.(xls 格式不受影响) 解决方案: 下载代码(红色为新添代码) public void download() { string fileName ...
- vim保存文件时,生成.un~文件
在用vim保存文件时,文件夹下生成.un~文件 怎么删除这些文件呢 在网上搜索的答案: http://stackoverflow.com/questions/15660669/what-is-a-un ...
- nio实现文件读取写入数据库或文件
1.nio实现读取大文件,之后分批读取写入数据库 2.nio实现读取大文件,之后分批写入指定文件 package com.ally; import java.io.File; import java. ...
- SpringBoot读取Linux服务器某路径下文件\读取项目Resource下文件
// SpringBoot读取Linux服务器某路径下文件 public String messageToRouted() { File file = null; try { file = Resou ...
随机推荐
- kafka的并行度与JStorm性能优化
kafka的并行度与JStorm性能优化 > Consumers Messaging traditionally has two models: queuing and publish-subs ...
- linux php.ini又一次载入问题
今天发现自己server改动php.ini之后无法又一次载入! .无法使用php-fpm reload,奇怪.! 后来查了一下.能够使用 /etc/init.d/php-fpm reload 来又一次 ...
- com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: serviceError([class java.lang.String]) 异常
在使用spring cloud 的 Hystrix 后可能会遇到 如下截图错误: 后台代码如下: 找了好一会经过分析参数方法和原方法参数步一致造成: 修改后代码如下:
- 高次同余方程模板BabyStep-GiantStep
/************************************* ---高次同余方程模板BabyStep-GiantStep--- 输入:对于方程A^x=B(mod C),调用BabySt ...
- 串 2016Vijos省选集训 day3[AC自动机]
1.串(string.c/.cpp/.pas) 限时1s,内存限制256MB,20个测试点 [题目描述] 兔子们在玩字符串的游戏.首先,它们拿出了一个字符串集合S,然后它们定义一个字符串为“好”的,当 ...
- 【BZOJ5018】[Snoi2017]英雄联盟 背包
[BZOJ5018][Snoi2017]英雄联盟 Description 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不了网友们的嘲讽,决定变强了 ...
- Throwing Dice(概率dp)
C - Throwing Dice Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Lig ...
- EasyNVR无插件直播服务器软件使用详情功能 - 录像功能说明
背景介绍 EasyNVR不仅仅拥有无插件的直播功能,更拥有对于直播录像的存储和日期检索功能: 本篇博文主要用于介绍EasyNVR的录像功能. 之前有博文介绍相关的录像功能,本篇主要为了介绍录像的新功能 ...
- 九度OJ 1354:和为S的连续正数序列 (整除)
时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:2028 解决:630 题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不 ...
- 关于iOS的手势UIGestureRecognizer问题
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { UIGestureRecognizerStatePossible, // 尚未识别是何种手 ...