Strtok No Longer Works, All Of A Sudden


Alex.

Retired
Joined
Aug 24, 2005
Messages
4,616
I am reading some lines from a text file, and I need to tokenize them. I used strtok() in the past successfully, but now it's causing me problems. The overall line is what I expect it to be, but each of its tokens are "0" (see code below).

The scary part is that a program that I made a while ago, that used strtok, no longer works on my GP2X (and it's the same old binary that worked a couple of days before that doesn't work now!).

CODE
#include <string.h>

...

char line[80] = "1,0,0,0,0,0,2,2,1,0";
char special[] = ",";
char* token;
int i;

for(i = 0; i < 10; i++) {
token = strtok(line, special); // token is "0" all the time!
}


I'm kind of stumped :-/
 
Alex. said:
I am reading some lines from a text file, and I need to tokenize them. I used strtok() in the past successfully, but now it's causing me problems. The overall line is what I expect it to be, but each of its tokens are "0" (see code below).

The scary part is that a program that I made a while ago, that used strtok, no longer works on my GP2X (and it's the same old binary that worked a couple of days before that doesn't work now!).

CODE
#include <string.h>

...

char line[80] = "1,0,0,0,0,0,2,2,1,0";
char special[] = ",";
char* token;
int i;

for(i = 0; i < 10; i++) {
token = strtok(line, special); // token is "0" all the time!
}


I'm kind of stumped :-/


I think it's pointer pb, your token are alway at last &line whish is "0"
I've never used this string function :)
 
Last edited by a moderator:
Try this :

CODE

char line[] = "1,0,0,0,0,0,2,2,1,0";
char special[] = ",";
char* token = NULL;
int i;

token = strtok(line, special);

while( token != NULL )
{
token = strtok( NULL, special);
}



strtok() points to the next token in the string so you don't keep calling it on the same string.
 
Tealion said:
Try this :

CODE

snip
strtok() points to the next token in the string so you don't keep calling it on the same string.

That's the trouble! Teaches me right for pasting old code around :D I suppose the other binary not working is a coincidence, maybe I deleted a resource file or something by accident.

Thanks a lot everyone!
 
Last edited by a moderator:
Back
Top