0

I have searched a lot about it on SO and solutions like "" the part where comma is are giving errors. Moreover it is using C++ :)

char *msg = new char[40];
msg = "1,2, Hello , how are you ";

char msg2[30];

strcpy_s(msg2, msg);

char * pch;
pch = strtok(msg2, ",");

while (pch != NULL)
{
    cout << pch << endl;
    pch = strtok(NULL, ",");
}

Output I want :

1
2
Hello , how are you

Out put it is producing

1
2
Hello 
 how are you

I have tried putting "" around Hello , how are you. But it did not help.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Shahan
  • 1
  • 2
  • Not related to your problem but `char *msg = new char[40]; msg = "1,2, Hello , how are you ";` is a blatant memory leak, you didn't fill the allocated buffer, but changed the pointer so the buffer has become unreachable. and [`strcpy_s`](http://en.cppreference.com/w/c/string/byte/strcpy) needs an extra parameter to protect the destination. – stefaanv Oct 18 '17 at 07:12

1 Answers1

2

The CSV files are comma separated values. If you want a comma inside the value, you have to surround it with quotes.

Your example in CSV, as you need your output, should be:

 msg = "1,2, \"Hello , how are you \"";

so the value Hello , how are you is surrounded with quotes.

This is the standard CSV. This has nothing to do with the behaviour of the strtok function.

The strtok function just searches, without considering anything else, the tokens you have passed to it, in this case the ,, thus it ignores the ".

In order to make it work as you want, you would have to tokenize with both tokens, the , and the ", and consider the previous found token in order to decide if the , found is a new value or it is inside quotes.

NOTE also that if you want to be completely conforming with the CSV specification, you should consider that the quotes may also be escaped, in order to have a quote character inside the value term. See this answer for an example: Properly escape a double quote in CSV

NOTE 2: Just for completeness, here is the CSV specification (RFC-4180): https://www.rfc-editor.org/rfc/rfc4180

Community
  • 1
  • 1
LoPiTaL
  • 2,495
  • 16
  • 23
  • It is giving wrong output if the user is also passing \" in message – Shahan Oct 18 '17 at 09:15
  • 1
    yes, `strtok` alone will not give your desired result. You have to process the tokens (both `,` AND `"`) and put some logic (`if`s and the like) in order to check which token has been found and if you are inside quotes, outside the quotes, etc... It is not as trivial as just using `strtok`. – LoPiTaL Oct 18 '17 at 11:50