32

I've seen of this thread: How can I read and parse CSV files in C++?

but it seems silly to reinvent the wheel for something as universal as a CSV parser. Boost has some routines which facilitate parsing, but doesn't have something out of the box.

Elsewhere, I see recommended libraries that are tied to .NET or otherwise platform specific. It's hard to believe that there isn't a preferred open C++ library to do something for this routine.

Any recommendations?

Community
  • 1
  • 1
daj
  • 6,962
  • 9
  • 45
  • 79

1 Answers1

-29

There's no "preferred" library for CSV parsing because it is less effort to write your own parser in C++ than to download some library, read how to use it, link it with your code, potentially encounter some bugs, change code to suit your needs etc. CSV parsing is trivial, there are three things you need to do:

  • Detect field delimiter.
  • Detect row delimiter.
  • Skip delimiters which are inside quotes.

Also there's an issue with file encoding, which delimiters you want to use, extra spaces and empty lines in file, and so on.

If you still want to use "preferred" CSV parser then maybe you should completely skip coding in C++ and move to some other language.

BJovke
  • 1,737
  • 15
  • 18
  • 19
    It might be trival to write a CSV parser for one file, but to write a general CSV parser that accepts a wide variety of different CSV formats is a major effort. Some programs have one or more comment lines at the top of their CSV output line, some have a header line, other have the headers inside of the comments. Some programs use comma as decimal point other use a point. And there are many other things you could think of. Is text denoted by single, double qoutes or maybe even in curley braces? Writing the unit tests for all those combinations is much work. – quinmars Nov 08 '16 at 20:12
  • 1
    You cannot "auto-detect" the format of CSV file. It's too simple by design. You have to know the structure in advance (what are the limiters, are there any special comment lines, etc.) of the file you want to process, there's no way to have some "universal" library that somehow detects the format of each CSV file. If you will ever use a spreadsheet program like Microsoft Excel you will see that there's a default format Excel expects and for different format you need to manually set all the parameters. – BJovke Nov 08 '16 at 21:54