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?