I am trying to get ALL tokens in a string using strtok()
and convert them to integers. After getting one token, trying to pull another promptly segfaults - how do I tell the system that this is not a segfault condition so it can complete?
Code:
char * token;
while ( getline (file,line) )
{
char * charstarline = const_cast<char*>(line.c_str()); //cast to charstar
char * token;
token = strtok(charstarline," ");
token = strtok(charstarline," ");
int inttoken = atoi(token);
cout << "Int token: " << inttoken << endl;
while (token != NULL)
{
token = strtok (NULL, " ");
int inttoken = atoi(token);
cout << "Int token (loop): " << inttoken << endl;
}
Is casting away const why it segfaults? If so how do I get around this?