Using std::quoted
allows you to read quoted strings from input streams.
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::stringstream ss;
ss << "\"Primary, Secondary, Third\", \"Primary\", , \"Secondary\", 18, 4, 0, 0, 0";
while (ss >> std::ws) {
std::string csvElement;
if (ss.peek() == '"') {
ss >> std::quoted(csvElement);
std::string discard;
std::getline(ss, discard, ',');
}
else {
std::getline(ss, csvElement, ',');
}
std::cout << csvElement << "\n";
}
}
The caveat is that quoted strings are only extracted if the first non-whitespace character of a value is a double-quote. Additionally, any characters after the quoted strings will be discarded up until the next comma.