Search code examples
c++stringstring-parsing

How strtok handles newlines?


I am trying to read data as tokens from a string, containing several lines. I use the code

    char * pch;
    pch = strtok ((char*)MyStr.c_str()," ,|,\n"); int i = 0;
    while (pch != NULL && i++ < 10)
    {
      cerr << i << ':' << pch << ' ';
      pch = strtok (NULL, " ,.-");
    }

The input is

std::string SP1271 = "1271,1\n"
"0,44248,8,45040,20,1,0,100\n"
"545,590,603,564,566,598,569,585,586,578\n";

and the output is

1:1271 2:1
0 3:44248 4:8 5:45040 6:20 7:1 8:0 9:100
545 10:590 

Is it legal to use '\n' as separator?


Solution

  • It's a very bad idea to use strtok() because this function might alter the original string, which is not supposed to be changed, because it's const.

    Also, even if this could work, you didn't foresee '\n' as token separator in the loop of subsequent strtok(). So better align all your token separator strings.

    Why not use C++ functionality:

    size_t pe;
    for (size_t pb=0; pe=MyStr.find_first_of(" ,-\n", pb); pb=pe+1)
    {
        cout  << MyStr.substr(pb, (pe==string::npos ? pe:pe-pb))<< " ";
        if (pe==string::npos)
            break; 
    }