How to use csvpp
How to use CSVPP
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <sstream> #include "csvpp.h" using namespace std; using namespace csvpp; int main() { RowReader tmp; stringstream ss; ss << "field1,field2,field3\r\n123,234,345\r\n999,000,111\r\n" ; ss >> tmp; rowiterator it; while (ss >> tmp) { for (it = tmp.begin(); it != tmp.end(); it++) cout << it->first << " => " << it->second << endl; cout << endl; } return 0; } |
Example 2, using a file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #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 >> tmp) { //rw.push_back(tmp); for (it = tmp.begin(); it != tmp.end(); it++) cout << it->first << " => " << it->second << endl; cout << endl; } return 0; } |