Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtok problem - strcmp

Reply
Thread Tools

strtok problem - strcmp

 
 
collinm
Guest
Posts: n/a
 
      03-24-2005
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?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-24-2005


collinm wrote:
> [...]
> 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().

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-24-2005
collinm wrote:
> 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
>
> if(strcmp(tokenptr,"HLD")==0)
> printf("hold\n");
> }
> }
>
> i get a segmentation fault
>
> any idea?
>



--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
John Valko
Guest
Posts: n/a
 
      03-24-2005
collinm wrote:
> 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
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-24-2005
On 24 Mar 2005 09:22:36 -0800, in comp.lang.c , "collinm"
<(E-Mail Removed)> wrote:

> tokenptr = strtok(NULL,seperators); /* get next token */


if strtok returned NULL here...
>
> if(strcmp(tokenptr,"HLD")==0)


.....this will segfault

you need to check for NULL before using the pointer
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Strange strcmp() problem Chad C Programming 8 06-18-2010 03:52 PM
weird problem with strcmp() rabbits77 C Programming 20 04-11-2010 10:16 AM
Using strcmp() after using strtok() ern C Programming 3 09-22-2005 03:47 PM
strcmp muser C++ 6 10-09-2003 08:18 AM
strcmp problem Shane Peck C++ 6 09-22-2003 05:44 PM



Advertisments