Skip to content

Instantly share code, notes, and snippets.

@sevanspowell
Created April 23, 2015 13:57
Show Gist options
  • Save sevanspowell/782a9b569f0cd43a17c9 to your computer and use it in GitHub Desktop.
Save sevanspowell/782a9b569f0cd43a17c9 to your computer and use it in GitHub Desktop.
// CSV contents is a 2D array, each line has it's own row and each row is made up of the fields found in that line.
Vector< Vector<std::string> > csvContents;
std::ifstream infile(fileName.c_str());
// Load elements of CSV into Vector.
while(infile.good())
{
std::string line;
std::getline(infile, line);
std::stringstream lineStream(line);
Vector<std::string> lineFields;
while (lineStream.good())
{
std::string subStr;
// Remove initial whitespaces
while (lineStream.peek() == ' ')
{
lineStream.get();
}
std::getline(lineStream, subStr, ',');
lineFields.append(subStr);
}
csvContents.append(lineFields);
}
// For each line, read contents
for (unsigned long int i = 0; i < csvContents.size(); ++i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment