-2

I am using CSV file with comma as delimiter

getline(strGetLine, word, ',')

I have a problem while reading something which as comma in between

Example -"mysentence,fromCSV"

Please let me know how to handle it.

sam dick
  • 1
  • 2
  • 1
    I believe your best option is to use a third party library to avoid different edge cases. If this is not on the table, my suggestion would be to read a whole line with getline(default newline delimiter), keep track of opened/closed quotes and ignore commas that are in opened quotes. Note that this is quite error-prone and makes the code much harder to read and maintain. – Ivaylo Strandjev Nov 03 '17 at 09:29
  • 1
    Searching would show that this has been asked and answered many times over, e.g. [C++ CSV line with commas and strings within double quotes](https://stackoverflow.com/questions/35639083/c-csv-line-with-commas-and-strings-within-double-quotes) – underscore_d Nov 03 '17 at 09:32
  • 1
    The call you're showing above doesn't match the interface of `getline()` from `stdio.h`, and you don't give a hint which environment you're using. Furthermore your question is missing a [mcve]. – Murphy Nov 03 '17 at 09:34
  • In particular, once you start allowing double quote to escape commas, you start wanting to find a way to escape double quotes. [RFC 4180](https://tools.ietf.org/html/rfc4180) specifies they should be doubled (note that it also specifies you can use double-quotes to include line end in a field). – Martin Bonner supports Monica Nov 03 '17 at 09:35
  • 1
    Well spotted! @underscore_d Dupe hammered. – Martin Bonner supports Monica Nov 03 '17 at 09:37

1 Answers1

1

I would recommend using a library, since this is not a new problem and has been solved several times.

The first example I can find is https://github.com/ben-strasser/fast-cpp-csv-parser, but you could also do something with Boost Spirit if you felt inclined.

lxop
  • 7,596
  • 3
  • 27
  • 42