Cocos2d C + + parsing CSV

Source: Internet
Author: User
Tags sin

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.