Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data

Status
Not open for further replies.

mayu03

Programmer
Oct 3, 2003
91
US
I have code where I am reading file. After I get the line into buffer I am trying to find first occurence of '*'.
How do I get the data from which is before the first occurence of '*'?
inFile.getline(inBuffer,'~');
char* p = strchr(inBuffer, '*');
 
char szDataBeforeFirstStar [255];
char* p = strchr(inBuffer, '*');
if (p)
{
strncpy(szDataBeforeFirstStar,inBuffer, p-inBuffer);
szDataBeforeFirstStar[p-inBuffer] ='\0';
}
Of corse, you should take care that int len=p-inBuffer is less than 255.

-obislavu-
 
One more question.
I add this line to compare the strings.
if (strcmp(szDataBeforeFirstStar,"STD")==0)
{ ??? }
If this is true I need to read the data in this line between 3 and 4 occurence of *. Can you help me do this?
 
It could be done using only strchr() but here is a solution to split the buffer using the strtok():
Code:
int iCountStar =0;
char szFirstToken[100];
char szAnotherToken[100];
char *pszToken = strtok(inBuffer,"*");
while (pszToken)
{
	iCountStar++;
	if (iCountStar == 1)
		strcpy(szFirstToken,pszToken);
	else if (iCountStar == 4)
		strcpy(szAnotherToken,pszToken);
	pszToken = strtok(0,"*");
}// end parsing
//Now you can check the values
if (!strcmp(szFirstToken,"STD"))
{
	if (strcmp(szAnotherToken,"SOMETHING"))
	{
	// printf("%s\n",szAnotherToken);
	}
}

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top