strtok problem - strcmp

C

collinm

hi

this is my code to analyse a file

void analyzeFilename()
{
char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
char *tokenptr;
char *seperators="_";

tokenptr = strtok(string,seperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n",tokenptr); /* print first token */
tokenptr = strtok(NULL,seperators); /* get next token */

}
}


that work fine

now i want to compare the token string, i do:


void analyzeFilename()
{
char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
char *tokenptr;
char *seperators="_";

tokenptr = strtok(string,seperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n",tokenptr); /* print first token */
tokenptr = strtok(NULL,seperators); /* get next token */

if(strcmp(tokenptr,"HLD")==0)
printf("hold\n");
}
}

i get a segmentation fault

any idea?
 
E

Eric Sosman

collinm said:
[...]
now i want to compare the token string, i do:


void analyzeFilename()
{
char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
char *tokenptr;
char *seperators="_";

tokenptr = strtok(string,seperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n",tokenptr); /* print first token */
tokenptr = strtok(NULL,seperators); /* get next token */

if(strcmp(tokenptr,"HLD")==0)
printf("hold\n");
}
}

i get a segmentation fault

Do the strcmp() *before* strtok().
 
M

Michael Mair

collinm said:
hi

this is my code to analyse a file

void analyzeFilename()
{
char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
char *tokenptr;
char *seperators="_";

tokenptr = strtok(string,seperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n",tokenptr); /* print first token */
tokenptr = strtok(NULL,seperators); /* get next token */

}
}


that work fine

now i want to compare the token string, i do:


void analyzeFilename()
{
char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
char *tokenptr;
char *seperators="_";

tokenptr = strtok(string,seperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n",tokenptr); /* print first token */
tokenptr = strtok(NULL,seperators); /* get next token */

tokenptr will be NULL in the very last loop iteration at this
point; thus, using it after this line will give you undefined
behaviour (and in your case, luckily, a segfault).
Put the call to strtok() at the end of the loop.

I have not looked to closely at your code nor tested it.

--Michael
 
J

John Valko

collinm said:
void analyzeFilename()
{
char string[]="B_L2_HLD_GRN_NOR_Run_Counter.txt";
char *tokenptr;
char *seperators="_";

tokenptr = strtok(string,seperators); /* get the first token
*/
while (tokenptr != NULL) /* while more tokens in
strng */
{
printf("\t%s\n",tokenptr); /* print first token */
tokenptr = strtok(NULL,seperators); /* get next token */
Consider the condition that breaks your loop -- when strtok returns
NULL. But before that condition is reached, you use the returned value
without checking it. My guess is that it's returning NULL and then
you're passing the NULL to strcmp() which causes the segmentation fault.
if(strcmp(tokenptr,"HLD")==0)
printf("hold\n");
}
}

i get a segmentation fault

any idea?

hope that helps,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top