Click here to Skip to main content
15,937,003 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys,
this morning i got this prorblem while parsing my text file.
I have simple code to parse string is as follow,
C++
const char* LastLineOfFile = FinalExecutionOfJob[NumberEntriesInLastExecution+1];
char * pch;
char* TempStr = strstr((char*)LastLineOfFile,""); 
const char* list[5];
pch = strtok (TempStr," ");
const char* ListOfToken[5];
int tokIndex = 0;
while (pch != NULL)
{
	pch = strtok (NULL, " ");
	switch(tokIndex)
         {
	         case 0:
			ListOfToken [0] = pch;
			break;
		case 1:
			ListOfToken [1] = pch;
			break;
		case 2:
			ListOfToken [2] = pch;
			break;
		case 3:
		         ListOfToken [3] = pch;
			break;
		case 4:
			ListOfToken [4] = pch;
			break;
	}
	tokIndex++;
}			

For String like,
"03-May-2013 18:04:03 service 105 snapshots were destroyed"

I need list[4] = "snapshots were destroyed";
But as usual i tokenize on the basis of " "(Space) so get string is breaking format that is in three parts like,
Snapshots
were
destroyed
i need all of this in sinble line.
Posted
Updated 7-May-13 18:29pm
v2
Comments
Richard MacCutchan 8-May-13 11:03am    
Why are you using a switch case when a simple for loop would do it better?

1 solution

This is not possible with strtok because this function inserts a NULL character at the end of the token.
You could do the following to get what you want -
C#
pch = strchr(TempStr, ' ');
pch = strchr(pch + 1, ' ');
pch = strchr(pch + 1, ' ');
pch = strchr(pch + 1, ' ');

Another thing I noticed in your code is that the first time, strtok would be called twice before storing in the list.
So you may want to place the strtok after the switch statement.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900