I know this is late but perhaps someone finds this useful:
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
namespace {
enum class State {
QUOTED,
UNQUOTED
};
}
int main()
{
string line;
State state = State::UNQUOTED;
using Field = string;
using Record = vector<Field>;
vector<Record> records;
string headerLine;
getline(cin, headerLine);
uint32_t p, q, r, lineno = 0;
while (getline(cin, line)) {
p = q = r = 0;
Record record;
for (auto c : line) {
switch (state) {
case State::UNQUOTED:
switch (c) {
case '\"':
state = State::QUOTED;
q = p = r + 1;
break;
default:
//assert(!"error");
break;
}
break;
case State::QUOTED:
switch (c) {
case '\"':
state = State::UNQUOTED;
q = r;
assert((q - p) > 0);
record.emplace_back(line.substr(p, (q - p)));
break;
default:
break;
}
break;
}
++r;
}
records.emplace_back(std::move(record));
}
auto recno = 0;
for (auto& r : records) {
cout << "Record " << ++recno << " with " << r.size() << " fields: ";
copy(r.cbegin(), r.cend(), ostream_iterator<string>(cout, "|"));
cout << endl;
}
return 0;
}
mb@mbp untitled % clang++ main.cpp -o main
mb@mbp untitled % curl -s "https://ir.eia.gov/wpsr/table9.csv" | ./main | head
Record 1 with 8 fields: Crude Oil Production |Domestic Production|12,200|12,100|11,700|11,100|12,125|11,550|
Record 2 with 8 fields: Crude Oil Production |Alaska|450|444|454|512|448|450|
Record 3 with 8 fields: Crude Oil Production |Lower 48|11,700|11,700|11,200|10,600|11,700|11,100|
Record 4 with 8 fields: Refiner Inputs and Utilization |Crude Oil Inputs|16,585|16,638|15,785|14,436|16,446|15,613|
Record 5 with 8 fields: Refiner Inputs and Utilization |East Coast (PADD 1)|760|809|742|612|803|723|
Record 6 with 8 fields: Refiner Inputs and Utilization |Midwest (PADD 2)|3,982|3,859|3,960|3,472|3,841|3,821|
Record 7 with 8 fields: Refiner Inputs and Utilization |Gulf Coast (PADD 3)|8,954|9,135|8,350|7,922|8,971|8,402|
Record 8 with 8 fields: Refiner Inputs and Utilization |Rocky Mountain (PADD 4)|573|586|574|550|579|546|
Record 9 with 8 fields: Refiner Inputs and Utilization |West Coast (PADD 5)|2,315|2,249|2,159|1,880|2,252|2,123|
Record 10 with 8 fields: Refiner Inputs and Utilization |Gross Inputs|17,162|17,106|16,287|14,692|16,955|16,095|
Hope this helps