There may be many types of read data files in COCOS2DX, such as reading Xml,lua,csv,json, which can be used as a format for configuration data.
Recently used to read the CSV data file, so on the internet to find a bit about this technology blog. Sure enough, the big gods on the internet are not stingy,
Don't talk nonsense, directly on the code. The code is as follows (test pass, readable data):
. h header File
qlcsvfile.h//// Created by Quasi_lee on 15-7-7.////#ifndef __qlcsvfile__#define __qlcsvfile__#include <stdio.h> #include "cocos2d.h" using_ns_cc;using namespace Std;class qlcsvfile {public: qlcsvfile (); ~qlcsvfile (); Used to store data vector<vector<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);p ublic: //Open CSV file bool OpenFile (const char* FileName); Data const char* getData (int rows,int cols) based on rows and columns; Gets the column of the specified data subscript int findcolsdata (int cols,const char* value); Get total number of columns inline int getcols () {return cols;} Get Total row number inline int getRows () {return data.size ();}}; #endif/* Defined (__qlcsvfile__) */
. cpp Files
qlcsvfile.cpp////Created by Quasi_lee on 15-7-7.////#include "QLCSVFile.h" Qlcsvfile::qlcsvfile (): Fieldsep (","), Cols (0) {}//Gets the data for the specified row and column const char* qlcsvfile::getdata (int rows, int cols) {if (Rows < 0 | | rows >= data.siz E () | | Cols < 0 | | cols >= data[rows].size ()) {return ""; } return Data[rows][cols].c_str ();} Gets the column of the specified data subscript int qlcsvfile::findcolsdata (int cols, const char *value) {for (int i = 0; i < data.size (); i++) { if (strcmp (GetData (i, cols), value) = = 0) {return i; }} return-1;} Parse CSV file bool Qlcsvfile::openfile (const char *filename) {string pathkey = Ccfileutils::sharedfileutils () Fullpathforfilename (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 (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; void Qlcsvfile::stringsplit (const std::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-LASTP OS)); Lastpos = str.find_first_not_of (delimiters, POS); pos = str.find_first_of (delimiters, lastpos); }}void qlcsvfile::split (vector<string>& field,string line) {string fld; int i = 0; int j = 0; if (line.length () = = 0) {return; } do {if (I < 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 qlcsvfile::advquoted (const string &line, string &fld, int i) {int j = 0; FLD = ""; for (j = i; J < Line.length (); j + +) {if (line[j] = = ' "' && line[++j]! = '" ') { int k = Line.find_first_of (Fieldsep, J); if (K > Line.length ()) {k = Line.length (); } for (k-= j; k--> 0;) {FLD + = line[j++]; } break; } FLD + = Line[j]; } return J;} int Qlcsvfile::advplain (const string &line, string &fld, int i) {int j = 0; j = line.find_first_of (Fieldsep, i); if (J > Line.length ()) {j = line.length (); } FLD = string (line, I, j-i); return J;} Destructors, freeing memory QLCSvfile::~qlcsvfile () {for (int i = 0; i < data.size (); i++) {data[i].clear (); } data.clear ();}
Code: http://download.csdn.net/detail/u010170012/8911095
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(4) COCOS2DX read CSV data file