User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,814 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,525 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 397 | Replies: 2
Reply
Join Date: Mar 2008
Posts: 19
Reputation: sjgriffiths is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
sjgriffiths sjgriffiths is offline Offline
Newbie Poster

strtok

  #1  
Mar 25th, 2008
hello

I have the following
  1. while(fgets(b2b_rates_address,1000,in_fp) != NULL )
  2. {
  3. strcpy(rates_details[j].ig1, strtok(b2b_rates_address,","));
  4. printf("rates_details[j].ig7 = %s\n",rates_details[j].ig1 );
  5. strcpy(rates_details[j].ig2,strtok(NULL,","));
  6. printf("rates_details[j].ig7 = %s\n",rates_details[j].ig2 );
  7. strcpy(rates_details[j].ig3,strtok(NULL,","));
  8. printf("rates_details[j].ig7 = %s\n",rates_details[j].ig3 );
  9. j++;
  10. }
The issue i have is this:-
It works fine up to field ig3, this is due to the fact that this field may/may not be empty....the program then bombs out

How can I get round this? any code examples would be appreciated

Many Thanks
Last edited by Ancient Dragon : Mar 25th, 2008 at 8:31 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: strtok

  #2  
Mar 25th, 2008
You are not validating the return value of strtok(). It returns NULL if its at the end of the string when you call strtok() with a NULL first parameter (or some other error occurred). It would be more prudent to code it like this:
char* ptr;
while(fgets(b2b_rates_address,1000,in_fp) != NULL )
{
     if( (ptr = strtok(b2b_rates_address,","))
     {
            strcpy(rates_details[j].ig1, ptr);
            printf("rates_details[j].ig7 = %s\n",rates_details[j].ig1 );
            if( (ptr = strtok(NULL,",") )
            {
                  strcpy(rates_details[j].ig2,ptr);
                  printf("rates_details[j].ig7 = %s\n",rates_details[j].ig2 );
                  if( (ptr = strtok(NULL, ","))
                  {
                      strcpy(rates_details[j].ig3, ptr);
                      printf("rates_details[j].ig7 = %s\n",rates_details[j].ig3 );
                   }
           }
     }
     j++;
}
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: strtok

  #3  
Mar 25th, 2008
>this field may/may not be empty
strtok ignores empty fields, so if that field is empty, you're actually processing a null pointer because there are fewer fields than you're expecting. If you want to get an empty string for the empty field, don't use strtok.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC