By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

strtok problem - strcmp

P: n/a
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?

Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


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().

--
Er*********@sun.com

Nov 14 '05 #2

P: n/a
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.
Nov 14 '05 #3

P: n/a
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
Nov 14 '05 #4

P: n/a
On 24 Mar 2005 09:22:36 -0800, in comp.lang.c , "collinm"
<co*****@laboiteaprog.com> 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>
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.