C++ CSV Parser


The function featured here is of course csvline_populate. It parses a line of data by a delimiter. If you pass in a comma as your delimiter it will parse out a Comma Separated Value (CSV) file. If you pass in a '\t' char it will parse out a tab delimited file (.txt or .tsv). CSV files often have commas in the actual data, but accounts for this by surrounding the data in quotes. This also means the quotes need to be parsed out, this function accounts for that as well.


It would make some sense to only pass in the line and delimiter to the function, and have the return type be the vector. However in terms of performance under heavy loads, this makes less sense. Passing in a predefined vector allows a function to populate it, copying bytes from your line as it goes. However, to not pass in a predefined vector, we'd have to declare it as a local variable to the function, which declares it on the stack. This means when the function completes, the variable will be deallocated, so when it returns the vector, the return keyword uses the copy constructor of the vector (which uses the copy constructor of each string object) to assign the return type to the variable in the caller function. To copy the return value of an object (depending on the size of your vector and the number of times you do it), can be an expensive operation.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
 
using namespace std;
 
void csvline_populate(vector<string> &record, const string& line, char delimiter);
 
int main(int argc, char *argv[])
{
    vector<string> row;
    string line;
    ifstream in("input.csv");
    if (in.fail())  { cout << "File not found" <<endl; return 0; }
 
    while(getline(in, line)  && in.good() )
    {
        csvline_populate(row, line, ',');
        for(int i=0, leng=row.size(); i<leng; i++)
            cout << row[i] << "\t";
        cout << endl;
    }
    in.close();
    return 0;
}
 
void csvline_populate(vector<string> &record, const string& line, char delimiter)
{
    int linepos=0;
    int inquotes=false;
    char c;
    int i;
    int linemax=line.length();
    string curstring;
    record.clear();
 
    while(line[linepos]!=0 && linepos < linemax)
    {
 
        c = line[linepos];
 
        if (!inquotes && curstring.length()==0 && c=='"')
        {
            //beginquotechar
            inquotes=true;
        }
        else if (inquotes && c=='"')
        {
            //quotechar
            if ( (linepos+1 <linemax) && (line[linepos+1]=='"') )
            {
                //encountered 2 double quotes in a row (resolves to 1 double quote)
                curstring.push_back(c);
                linepos++;
            }
            else
            {
                //endquotechar
                inquotes=false;
            }
        }
        else if (!inquotes && c==delimiter)
        {
            //end of field
            record.push_back( curstring );
            curstring="";
        }
        else if (!inquotes && (c=='\r' || c=='\n') )
        {
            record.push_back( curstring );
            return;
        }
        else
        {
            curstring.push_back(c);
        }
        linepos++;
    }
    record.push_back( curstring );
    return;
}


Jason Anderson on 2009-04-09 03:14:02
Hey,
Awesome function, but you can remove the "int i;" line because i is not used. Just a FYI.

milkplus on 2009-12-15 00:21:16
very clean and excellent parser! one issue i saw was leading and trailing whitespace on the row strings is not trimmed off.

Roberto on 2010-03-03 12:01:42
Nice~

Roberto on 2010-07-07 23:50:32
I believe a far more efficient C++ CVS implementation can be found here: http://www.codeproject.com/KB/recipes/Tokenizer.aspx

Lis on 2010-12-18 15:53:00
Well the above lib makes heavy use of boost.
While this is just a simple 50 line function.

Great work.

Jeff B on 2011-04-19 05:29:35
Great little function. So simple.

Bernard on 2011-05-30 14:40:16
Nice. Thanks.

Runia on 2011-06-05 17:33:23
Thank you for sharing the csv parser. It works perfectly.
Maybe http://www.codeproject.com/KB/recipes/Tokenizer.aspx is more efficient and the ideas behind are nice but it is not as easily applied neither does it yield immediate results like your code. Thank you.

Jan on 2013-02-22 09:53:36
I'm sorry to say, but it does not work for multiple quoted fields. Like
"Aaaa", "Bbbb"