-2

I am trying to token a string and I'm getting some strange behaviour. Here is the code:

ifstream fin("myFile.txt");
char* in[256];
fin.getline(in,256);
string s = in;
vector<string> f;
f.push_back(s);
char* pch1,pch2;
pch1 = strtok((char*)s.c_str()," ");
pch1 = strtok(NULL," ");
pch2 = strtok((char*)f[0].c_str()," ");
pch2 = strtok(NULL," ");

if the file contains the line "This is a string", then the value stored at location pch1 will be "is", but pch2 will point to null. Why is that?

I printed out the bytes of the two strings, s and f[0]. s contains all the characters I expect, including the space. However, f[0] contains the 0 character instead of spaces. I understand that sometimes istreams can treat the space as a null character, but shouldn't s and f[0] contain exactly the same bytes?

Michael Stachowsky
  • 717
  • 1
  • 5
  • 21

2 Answers2

5

strtok modifies the string, it replaces occurrences of the delimiter with '\0'. c_str() returns a const char*, you should not simply cast the const away.

alain
  • 11,939
  • 2
  • 31
  • 51
  • 3
    Yup, in all cases, modifying a `const` variable via non-`const` reference/pointer results in UB. That's probably just one of the problems with this attempt. – underscore_d Feb 17 '16 at 15:24
2

You are causing all kinds of undefined behavior. I would recommend staying away from stroke and instead look into boost's tokenizer library. Your brain cells will appreciate it. Just forget that the entire C library exists in C++ unless you really, really, really need it to talk to another C library.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125