3

I am trying to read data from a text file and split the read line based on quotes. For example

"Hi how" "are you" "thanks"

Expected output

Hi how
are you
thanks

My code:

getline(infile, line); 
ch = strdup(line.c_str());
ch1 = strtok(ch, " ");

while (ch1 != NULL)
{
    a3[i] = ch1;
    ch1 = strtok(NULL, " ");
    i++;
}

I don't know what to specify as delimiter string. I am using strtok() to split, but it failed. Can any one help me?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    We need code to help. Anyway, there are much better methods than `strtok`. For example, some of [these](http://stackoverflow.com/questions/236129/splitting-a-string-in-c). – chris Apr 10 '13 at 05:15
  • no i have text with in quotes like i specify above and i need the expected output which i mention above.In delimiter i specify space but that was not correct.At that point only i have problem –  Apr 10 '13 at 05:26
  • Oops, your comment that was previously in the code messed me up for some reason, sorry. – chris Apr 10 '13 at 05:27

2 Answers2

2

Please have a look at the example code here. You should provide "\"" as delimiter string to strtok. For example,

ch1 = strtok (ch,"\"");

Probably your problem is related with representing escape sequences. Please have a look here for a list of escape sequences for characters.

Atiq Rahman
  • 680
  • 6
  • 24
0

Given your input: "Hi how" "are you" "thanks", if you use strtok with "\"" as the delimiter, it'll treat the spaces between the quoted strings as if they were also strings, so if (for example) you printed out the result strings, one per line, surrounded by square brackets, you'd get:

[Hi how]
[ ]
[are you]
[ ]
[thanks]

I.e., the blank character between each quoted string is, itself, being treated as a string. If the delimiter you supplied to strtok was " \"" (i.e., included both a quote and a space) that wouldn't happen, but then it would also break on the spaces inside the quoted strings.

Assuming you can depend on every item you care about being quoted, you want to skip anything until you get to a quote, ignore the quote, then read data into your input string until you get to another quote, then repeat the whole process.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111