1. What is CSV
Id, theme level name, theme background music, theme background image, 1, level name 1,test.mp3,test.png,2, level name 2,test.mp3,test.png,3, level name 3,test.mp3,test.png,
the file is in English ', ' as a delimiter. This structure is a bit like the structure of the database table, because it is very simple, so the scope of application is relatively broad, Excel can export CSV, Sqlite and other databases can also export CSV.
In the game development, this file is generally for the planning to edit, after modifying the value is easy to test, do not need to compile the game. I personally prefer the script development, let the plan to modify the script directly, save the CSV this part.
2. Parsing csv
There are many versions on the network, which are too complex to write. Some don't work very well yet. I think the good version is only a C + + STL, and the returned value should be a two-dimensional array of strings vector<vector<string> >. I wrote the following:
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;}
Use (Cocos2d-x 3.x version):
Csvparser parser = Csvparser (Fileutils::getinstance ()->fullpathforfilename ("Test.csv"). C_STR ()); vector<vector<string> > data = parser.data;
http://www.waitingfy.com/archives/1722
Cocos2d C + + parsing CSV