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 427,204 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 2,142 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: Programming Forums
Views: 232 | Replies: 6
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  
Apr 14th, 2008
Hello

char **pOutData; /* Output results */
char *ptoken

I have a pointer which contains the following
hdjhasdjkhasd;stephenjohnson;647823463;dhasdjhaj

I want to work through and get out just stephenjohnson

I have the following so far, which does not work....any help?
  1. for ( size = 0; size < b2b_data_drill_nNoSelect; size++ )
  2. {
  3. if (size == 1 )
  4. {
  5. ptoken = strtok(pOutData, ";" );
  6.  
  7. while( ptoken != NULL )
  8. {
  9. printf ("Results 2nd:%s\n", pOutData[size] );
  10. printf ("ptoken:%s\n", ptoken );
  11. ptoken = strtok(NULL, ";" );
  12.  
  13. }
  14. }
  15. }
Last edited by Ancient Dragon : Apr 14th, 2008 at 9:41 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,216
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: 38
Solved Threads: 933
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: strtok

  #2  
Apr 14th, 2008
pOutData appears to be a pointer to a pointer or a 2d array of strings, like argv parameter to main() In either event line 5 can not work because strtok() only works with 1d character arrays. pOutData needs to be a pointer that is declared similar to the way you declared pToken
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: 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

Re: strtok

  #3  
Apr 14th, 2008
Thanks for the reply

The issue i have is i cannot change pOutData due to it being part of some software we are linking too. So i have to make something work around it...any ideas?

Im really stuck here
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,216
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: 38
Solved Threads: 933
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: strtok

  #4  
Apr 14th, 2008
I would copy the string into some local buffer because strtok() will replace the ; with null characters, which destroys the original string.
char temp[255];
strcpy(temp,*pOutData);
pToke = strtok(temp,",");
// rest of program here
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: 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

Re: strtok

  #5  
Apr 14th, 2008
mmmm still an issue as

printf ("Results 2nd:%s\n", pOutData[size] );

Based on the above printf, i cannot just do a simple strcpy into temp, due to size being included can i?
Reply With Quote  
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

Re: strtok

  #6  
Apr 14th, 2008
ah it is working, except at the end the program is bombing out
for ( size = 0; size < b2b_data_drill_nNoSelect; size++ )  
{

      if (size == 1 )
      {
	strcpy(temp,pOutData[size]);
	//printf("Temp = %s\n", temp);
	ptoken = strtok(temp, ";" );
	while( ptoken != NULL )
	{
   	     printf ("Results 2nd:%s\n", pOutData[size] );	
  	     printf ("ptoken:%s\n", ptoken );	
	     ptoken = strtok(NULL, ";" );
	}
       }			
							
				
}


any ideas?
Last edited by sjgriffiths : Apr 14th, 2008 at 10:26 am.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,216
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: 38
Solved Threads: 933
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: strtok

  #7  
Apr 14th, 2008
>>except at the end the program is bombing out
Don't know -- probably something else in your program. Did you declare temp variable big enough to hold all the characters in that string ?

>>if (size == 1 )
Why that condition? Just delete the loop and do it like this, unless of course there is other code within that loop that you haven't posted.

	strcpy(temp,pOutData[1]);
	//printf("Temp = %s\n", temp);
	ptoken = strtok(temp, ";" );
	while( ptoken != NULL )
	{
   	     printf ("Results 2nd:%s\n", pOutData[1] );	
  	     printf ("ptoken:%s\n", ptoken );	
	     ptoken = strtok(NULL, ";" );
	}
 
Last edited by Ancient Dragon : Apr 14th, 2008 at 10:33 am.
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  
Reply

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

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

 

Thread Tools Display Modes

Other Threads in the C Forum

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