An easy-to-use CSV file parser for C++
Andrew DeOrio awdeorio@umich.edu
http://andrewdeorio.com
$ git clone https://github.com/awdeorio/csvstream.git
$ cd csvstream/
$ make test
This example reads one column from a CSV file.
// csvstream_example1.cpp
#include "csvstream.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
// Open file
csvstream csvin("csvstream_example.csv");
// Rows have key = column name, value = cell datum
map<string, string> row;
// Extract the "animal" column
while (csvin >> row) {
cout << row["animal"] << "\n";
}
}
Input
$ cat csvstream_example.csv
name,animal
Fergie,horse
Myrtle II,chicken
Oscar,cat
Compile
$ make csvstream_example1
# OR
$ g++ -std=c++11 csvstream_example1.cpp -o csvstream_example1
Output
$ ./csvstream_example1
horse
chicken
cat
This example has an outer loop for each row and an inner loop for each column.
//csvstream_example2.cpp
#include "csvstream.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
// Open file
csvstream csvin("csvstream_example.csv");
// A row is a map<string, string>, key = column name, value = datum
map<string, string> row;
// Read file
while (csvin >> row) {
cout << "row:" << "\n";
for (auto &col:row) {
const string &column_name = col.first;
const string &datum = col.second;
cout << " " << column_name << ": " << datum << "\n";
}
}
}
Input
$ cat csvstream_example.csv
name,animal
Fergie,horse
Myrtle II,chicken
Oscar,cat
Compile
$ make csvstream_example2
# OR
$ g++ -std=c++11 csvstream_example2.cpp -o csvstream_example2
Output
$ ./csvstream_example2
row:
animal: horse
name: Fergie
row:
animal: chicken
name: Myrtle II
row:
animal: cat
name: Oscar
This example uses a vector-of-pair to keep the order of the items read from the file.
/* csvstream_example3.cpp
*
* Andrew DeOrio <awdeorio@umich.edu>
*
* An easy-to-use CSV file parser for C++
* https://github.com/awdeorio/csvstream
*/
#include "csvstream.h"
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
int main() {
// Open file
csvstream csvin("csvstream_example.csv");
// A row is a vector<pair<string, string>>
// key = column name, value = cell datum
vector<pair<string, string>> row;
// Read file
while (csvin >> row) {
cout << "row:" << "\n";
for (auto &col:row) {
const string &column_name = col.first;
const string &datum = col.second;
cout << " " << column_name << ": " << datum << "\n";
}
}
}