We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,167 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

strtok

FILE *finp1 = file.txt 
--------------
web: add $1,$2,$3
     sub $4,$5,$6
end: .word 5
------------------

for some reason i can not go in my 2nd if statment. i think bc i am not using strtok right

------------------------------------------------code----------------

while(fgets(line, 20, finp1) != NULL)               //works
{
    tptr = strtok(line, ":");                          //works
        if(tptr != NULL)
           {
               if(strstr(line, ".word") == 0)        //i cant get in this if statment
                  {
                      tptr2 = strtok(line, " ");
                      tptr2 = strtok(NULL, "");
                      if(tptr2 != NULL)
                          {
                              //do some thing
                          }
                  }
           }
           else                                     //works
           {
           }
}
2
Contributors
1
Reply
3 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
hwoarang69
Posting Pro
569 posts since Feb 2012
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 7

When you called strtok at line 3 it replaced the ':' in line with a '\0' (because that is how it works). That reduces the string in line to "end" which does not contain ".word" and leaves tptr pointing at line. If you called strtok a second time then tptr will be updated to point at the rest of the string " .word 5" which does contain ".word".

On the whole I would recomend not using strtok at all because of this destruction of data and because it is not re-entrant. It is relatively easy to parse the strring yourself.

Banfa
Practically a Master Poster
695 posts since Mar 2010
Reputation Points: 508
Solved Threads: 109
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.2872 seconds using 2.67MB