csvpp

HowToUseCSVPP


You are looking at an old revision of the page HowToUseCSVPP. This revision was created by Natalie Adams.

Table of Contents

How to use csvpp

Details =

Inside of csvpp.h there are 3 different objects: * RowReader * RowWriter * rowiterator Each of these objects are located in the namespace csvpp.

Note - if you want to use fields you should have one stream statement before looping through the data For example:

fileistream >> tmp; //this populates the fields
while(!fileistream.eof())
fileistream >> tmp;

Otherwise you will end up trying to access an empty row.

Example 1, using a string:

#include <iostream>
#include <sstream>
#include "csvpp.h"

using namespace std;
using namespace csvpp;

int main()
{

    RowReader tmp;
        //RowWriter rw;
        stringstream ss;
        ss << "field1,field2,field3\r\n123,234,345\r\n999,000,111\r\n";
        ss >> tmp;
        rowiterator it;
        while(!ss.eof())
        {
                ss >> tmp;
                //rw.push_back(tmp);
                for(it = tmp.begin(); it != tmp.end(); it++)
                        cout << it->first << " => " << it->second << endl;
                cout << endl;
        }
        //cout << rw;
        return 0;

}

Example 2, using a file:

#include <iostream>
#include <fstream>
#include "csvpp.h"

using namespace std;
using namespace csvpp;

int main()
{
        ifstream f("sampledata.csv");
        RowReader tmp;
        //RowWriter rw;
        f >> tmp;
        rowiterator it;
        while(!f.eof())
        {
                f >> tmp;
                //rw.push_back(tmp);
                for(it = tmp.begin(); it != tmp.end(); it++)
                        cout << it->first << " => " << it->second << endl;
                cout << endl;
        }
        //cout << rw;
        return 0;
}

Created: 11 years 4 months ago
by Natalie Adams

Updated: 11 years 4 months ago
by Natalie Adams

Labels:
Other:Featured

Old Revisions

Page rendered in 0.02357s using 26 queries.