•
•
•
•
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
![]() |
•
•
Join Date: Mar 2008
Posts: 19
Reputation:
Rep Power: 1
Solved Threads: 0
hello
I have the following
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
I have the following
c Syntax (Toggle Plain Text)
while(fgets(b2b_rates_address,1000,in_fp) != NULL ) { strcpy(rates_details[j].ig1, strtok(b2b_rates_address,",")); printf("rates_details[j].ig7 = %s\n",rates_details[j].ig1 ); strcpy(rates_details[j].ig2,strtok(NULL,",")); printf("rates_details[j].ig7 = %s\n",rates_details[j].ig2 ); strcpy(rates_details[j].ig3,strtok(NULL,",")); printf("rates_details[j].ig7 = %s\n",rates_details[j].ig3 ); j++; }
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation:
Rep Power: 36
Solved Threads: 860
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- Segmentation fault using strtok (C)
- problems with strtok (C++)
- strtok() function questions <C programming> (C)
- Strtok() (C)
- piglatin program with fgets, strtok, and strlen (C)
Other Threads in the C Forum
- Previous Thread: i can't fix this seemingly simple error
- Next Thread: Calling a function involving arrays



Linear Mode