实现类

CCSVParse.h

  1. #ifndef __C_CSV_PARSE__
  2. #define __C_CSV_PARSE__
  3.  
  4. #include "cocos2d.h"
  5. #include <vector>
  6. using namespace std;
  7.  
  8. class CCSVParse
  9. {
  10. public:
  11. //CCSVParse(void);
  12. ~CCSVParse(void);
  13.  
  14. CCSVParse(istream& fin=cin, string sep=","):
  15. fieldsep(sep),
  16. cols()
  17. {
  18.  
  19. }
  20.  
  21. //用以存储数据
  22. std::vector<std::vector<std::string>> data;
  23.  
  24. private:
  25. string fieldsep;
  26. int cols;
  27.  
  28. void StringSplit(const string& str, vector<string>& tokens, const char& delimiters);
  29. void split(vector<string>& field, string line);
  30. int advplain(const string& line, string& fld, int);
  31. int advquoted(const string& line, string& fld, int);
  32.  
  33. public:
  34. bool openFile(const char* fileName);
  35. const char* getData(unsigned int rows, unsigned int cols);
  36. int findColsData(int cols, const char* value);
  37.  
  38. inline int getCols(){return cols;}
  39. inline int getRows(){return data.size();};
  40. };
  41.  
  42. #endif //__C_CSV_PARSE__

CCSVParse.cpp

  1. #include "CSVParse.h"
  2.  
  3. using namespace cocos2d;
  4.  
  5. // CCSVParse::CCSVParse(void)
  6. // {
  7. // }
  8.  
  9. CCSVParse::~CCSVParse(void)
  10. {
  11. }
  12.  
  13. void CCSVParse::StringSplit( const string& str, vector<string>& tokens, const char& delimiters )
  14. {
  15. string::size_type lastPos = str.find_first_not_of(delimiters, );
  16. string::size_type pos = str.find_first_of(delimiters, lastPos);
  17. while (string::npos != pos || string::npos != lastPos)
  18. {
  19. tokens.push_back(str.substr(lastPos, pos-lastPos));
  20. lastPos = str.find_first_not_of(delimiters, pos);
  21. pos = str.find_first_of(delimiters, lastPos);
  22. }
  23. }
  24.  
  25. void CCSVParse::split( vector<string>& field, string line )
  26. {
  27. string fld;
  28. unsigned int i,j=;
  29.  
  30. if( line.length() == )
  31. return;
  32. i=;
  33.  
  34. do
  35. {
  36. if(j<line.length() && line[i]=='"')
  37. j = advquoted(line, fld, ++i);
  38. else
  39. j = advplain(line, fld, i);
  40.  
  41. field.push_back(fld);
  42. i = j+;
  43. } while (j<line.length());
  44. }
  45.  
  46. int CCSVParse::advplain( const string& s, string& fld, int i)
  47. {
  48. unsigned int j;
  49. j = s.find_first_of(fieldsep, i);
  50. if(j>s.length())
  51. j=s.length();
  52. fld = string(s,i,j-i);
  53. return j;
  54. }
  55.  
  56. int CCSVParse::advquoted( const string& s, string& fld, int i)
  57. {
  58. unsigned int j;
  59. fld = "";
  60. for (j=i; j<s.length(); ++j)
  61. {
  62. if(s[j]=='"' && s[++j]!='"')
  63. {
  64. unsigned int k = s.find_first_of(fieldsep, j);
  65. if(k>s.length())
  66. k = s.length();
  67. for(k-=j; k-->;)
  68. fld += s[j++];
  69. break;
  70. }
  71. fld += s[j];
  72. }
  73. return j;
  74. }
  75.  
  76. //解析 CVS 文件
  77. bool CCSVParse::openFile( const char* fileName )
  78. {
  79. string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
  80. unsigned char* pBuffer = nullptr;
  81. unsigned long bufferSize = ;
  82. pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "r", &bufferSize);
  83.  
  84. string s = (char*)pBuffer;
  85. string str = s.substr(,bufferSize);
  86.  
  87. vector<string> line;
  88. StringSplit(str, line, '\n');
  89. for(unsigned int i=; i<line.size(); ++i)
  90. {
  91. vector<string> field;
  92. split(field, line[i]);
  93. data.push_back(field);
  94. cols = max(cols, (int)field.size());
  95. }
  96.  
  97. return true;
  98. }
  99.  
  100. //获取指定行列的数据
  101. const char* CCSVParse::getData(unsigned int rows, unsigned int cols )
  102. {
  103. if (rows< || rows>=data.size() || cols< || cols>=data[rows].size())
  104. {
  105. return "";
  106. }
  107. return data[rows][cols].c_str();
  108. }
  109.  
  110. //获取指定数据的列下标
  111. int CCSVParse::findColsData( int cols, const char* value )
  112. {
  113. for (unsigned int i=; i<data.size(); ++i)
  114. {
  115. if(strcmp(getData(i,cols),value)==)
  116. return i;
  117. }
  118. return -;
  119. }

HelloWorld.cpp 中 init()  函数中添加

  1. CCSVParse* csvFile = new CCSVParse();
  2. csvFile->openFile("Book1.csv");
  3. for (int i=; i<csvFile->getCols(); ++i)
  4. {
  5. string strLine = "";
  6. for(int j=; j<csvFile->getRows(); ++j)
  7. {
  8. strLine += csvFile->getData(i,j);
  9. strLine += ",";
  10. }
  11. CCLabelTTF* pLab = CCLabelTTF::create(strLine.c_str(),"Arial",);
  12. pLab->setPosition(ccp(size.width/,size.height--i*));
  13. this->addChild(pLab,);
  14. }

Book1.csv 内容  注意将文件保存为UTF-8格式的(否则中文显示乱码)

  1. 星期一,1,10000,HP1,MP1,数值1,Icon1.png
  2. 星期二,2,10001,HP2,MP2,数值2,Icon2.png
  3. 星期三,3,10002,HP3,MP3,数值3,Icon3.png
  4. 星期四,4,10003,HP4,MP4,数值4,Icon4.png
  5. 星期五,5,10004,HP5,MP5,数值5,Icon5.png
  6. 星期六,6,10005,HP6,MP6,数值6,Icon6.png
  7. 星期日,7,10006,HP7,MP7,数值7,Icon7.png

win32 平台 显示结果:(保存文件 非UTF-8格式)中文显示乱码

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

不早了,洗洗睡吧 , 明天又起不来了

cocos2d-x CSV文件读取 (Excel生成csv文件)的更多相关文章

  1. 使用OLEDB读取excel和csv文件

    这是我第一次在博客上写东西,简单的为大家分享一个oledb读取文件的功能吧,这两天在做一个文件导入数据库的小demo,就想着导入前先在页面上展示一下,之前调用Microsoft.Office.Inte ...

  2. 用PHP读取Excel、CSV文件

    PHP读取excel.csv文件的库有很多,但用的比较多的有: PHPOffice/PHPExcel.PHPOffice/PhpSpreadsheet,现在PHPExcel已经不再维护了,最新的一次提 ...

  3. 使用OLEDB方式 读取excel和csv文件

    /// <summary> /// 使用OLEDB读取excel和csv文件 /// </summary> /// <param name="path" ...

  4. 通过oledb驱动读取excel、csv数据丢失解决方案

    1.问题出现 在开发应用程序的过程中,比较常用一功能就是通过oledb驱动读取excel.csv.text等文件:而最近有客户反映,在使用短信平台(下载地址:http://www.sms1086.co ...

  5. 将CSV格式或者EXCEL格式的文件导入到HIVE数据仓库中

    学习内容:数据导入,要求将CSV格式或者EXCEL格式的文件导入到HIVE数据仓库中: ①hive建表:test1 create table test1 (InvoiceNo String, Stoc ...

  6. java读取字符串,生成txt文件

    /** * 读取字符串,生成txt 文件 已解决未设置编码时,在项目中直接打开文件,中文乱码问题 * WriteText.writeToText(musicInfo,fileName)直接调用 * * ...

  7. C# Aspose.Cells导出xlsx格式Excel,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”

    报错信息: 最近打开下载的 Excel,会报如下错误.(xls 格式不受影响) 解决方案: 下载代码(红色为新添代码) public void download() { string fileName ...

  8. vim保存文件时,生成.un~文件

    在用vim保存文件时,文件夹下生成.un~文件 怎么删除这些文件呢 在网上搜索的答案: http://stackoverflow.com/questions/15660669/what-is-a-un ...

  9. nio实现文件读取写入数据库或文件

    1.nio实现读取大文件,之后分批读取写入数据库 2.nio实现读取大文件,之后分批写入指定文件 package com.ally; import java.io.File; import java. ...

  10. SpringBoot读取Linux服务器某路径下文件\读取项目Resource下文件

    // SpringBoot读取Linux服务器某路径下文件 public String messageToRouted() { File file = null; try { file = Resou ...

随机推荐

  1. 什么是scale up和scale out

    Scale Out(也就是Scale horizontally)横向扩展,向外扩展Scale Up(也就是Scale vertically)纵向扩展,向上扩展无论是Scale Out,Scale Up ...

  2. Discrete Function(简单数学题)

    Discrete Function There is a discrete function. It is specified for integer arguments from 1 to N (2 ...

  3. [转]为 windows cmd 设置代理

    为 windows cmd 设置代理 转自:http://blog.csdn.net/lovelyelfpop/article/details/69586366 通过cmd命令行执行某些命令,如果这些 ...

  4. CSS 布局实例系列(三)如何实现一个左右宽度固定,中间自适应的三列布局——也聊聊双飞翼

    今天聊聊一个经典的布局实例: 实现一个三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化 可能很多朋友已经笑了,这玩意儿通过双飞翼布局就能轻松实现.不过,还请容我在双飞 ...

  5. 【译】Stackoverflow:Java Servlet 工作原理问答

    导读 本文来自stackoverflow的问答,讨论了Java Servlet的工作机制,如何进行实例化.共享变量和多线程处理. 问题:Servlet 是如何工作的?Servlet 如何实例化.共享变 ...

  6. centos6.9下设置nginx服务开机自动启动

    首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vi /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - ...

  7. amp模板展示amp网站也可以做得很好看

    ytkah比较喜欢研究一些新东西,AMP刚出来的时候就上手了,也做了一些站点,而且还不赖,因为这个还机缘巧合参加了深圳的谷歌全球合作伙伴大会,很多大牛也都来了,很荣幸能和他们一起交流.下面就稍微展示一 ...

  8. springcloud zuul 使用zuulfilter 修改请求路径和响应头

    最近做项目有一个需求:一个网盘系统,文件存放在分布式文件系统中,之前的文件下载统一走的文件下载服务,现在需要在单文件下载的时候不需要走文件下载服务,而是直接访问文件系统上的路径,响应的时候修改响应头, ...

  9. Android动画效果animation

    1.Tween 根据指定动画开始和结束时的对象属性(位置.Alpha值(透明度).大小.角度等)以及动画播放的时间长度生成动画: 2.Frame 指定每一帧所播放的图片和时间长度.   建立动画的方法 ...

  10. 目标检测--之RCNN

    目标检测--之RCNN 前言,最近接触到的一个项目要用到目标检测,还有我的科研方向caption,都用到这个,最近电脑在windows下下载数据集,估计要一两天,也不能切换到ubuntu下撸代码~.所 ...