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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse string using strstr or strch

Status
Not open for further replies.

spining123

Technical User
Jun 5, 2003
33
US

Question: How do you parse this string separating $a, $b, $c, and $d, into separate new fields. I know I will need to declare the field’s names; but not should of using the strstr or strch to separate the fields.

Example of the table:

typedef struct
{
char fieldA[50];
char fieldB[200];
} hrgtable;

tableA tabA[] = {
"1 $aSome data in field A","111$bPart1 of fieldB$cPart2 of fieldB$dPart3 of fieldB”,
"2 $aSome data in field A","222$bPart1 of fieldB$cPart2 of fieldB$dPart3 of fieldB”,
"3 $aSome data in field A","333$bPart1 of fieldB$cPart2 of fieldB$dPart3 of fieldB”,
};

Example of the code to parse string:

if (( m = strstr(tabA.fieldB,"$c")) !=NULL)
{
len = m - (tabA.fieldB);

printf("Testing... what's in m ^^^%s^^^\n",m);

This is what’s m… $cPart2 of fieldB$dPart3 of fieldB

I just want the string data starting at $c and ending at $d.
I did the following to copy the data starting at $d into n.

n = strchr(m,'$');
printf("Testing... what's in n ^^^%s^^^\n",n);
This is what’s n… $dPart3 of fieldB

I’m trying to get the data starting at $c and ending at $d strcat to textout.

strcat(textout,n - m);
};

I get the error message:
1506-280 (W) Function argument assignment between type
s "const unsigned char*" and "long" is not allowed.

Thank you in advance for your help.

 
n - m is a char* - char* which will give you an int. I think you want
Code:
strncat (textout, m, n -m);
Alternatively, if the code is non-reentrant and does not need to be thread safe, you could use strtok.
Code:
beforeBdol = strtok (m, "$") + 1;
beforeCdol = strtok (NULL, "$") + 1;
beforeDdol = strtok (NULL, "$") + 1;
strcat (textout, beforeCdol);
 
Thank you xwb,
I learned something new today (strtok).
I used the strtok as suggested and it resulted my problem. Again, thank you...
 
I'm not fond of [tt]strtok[/tt] because it modifies the original string. Toying with this, here is another approach.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int foo(char *dest, int size, const char *field, const char *delim)
{
   const char *end, *start = strstr(field, delim);
   if ( start != NULL )
   {
      start += strlen(delim);
      end = strchr(start, *delim);
      if ( end != NULL )
      {
         int length = end - start;
         if ( length >= --size )
         {
            length = size;
         }
         sprintf(dest, "%*.*s", length, length, start);
         return 1;
      }
   }
   return 0;
}

struct hrgtable
{
   char fieldA[50];
   char fieldB[200];
};

struct hrgtable tabA[] =
{
   {"1 $aSome data in field A","111$bPart1 of fieldB$cPart2-1 of fieldB$dPart3 of fieldB"},
   {"2 $aSome data in field A","222$bPart1 of fieldB$cPart2-2 of fieldB$dPart3 of fieldB"},
   {"3 $aSome data in field A","333$bPart1 of fieldB$cPart2-3 of fieldB$dPart3 of fieldB"},
};

int main(void)
{
   size_t i;
   for ( i = 0; i < sizeof tabA / sizeof *tabA; ++i )
   {
      char buffer[32];
      if ( foo(buffer, sizeof buffer, tabA[i].fieldB, "$c") )
      {
         puts(buffer);
      }
   }
   return 0;
}

/* my output
Part2-1 of fieldB
Part2-2 of fieldB
Part2-3 of fieldB
*/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top