paring char* using strtok

A

arssa2020

hello,
i am writing a GPS app in evc++ 4.0, i am using c-style char* instead
of CStrings, i want to parse NMEA sentence such as

"GPZDA,162913.48,19,08,2005,,*63"
problem is when i use strtok it ignores an empty data field
below is the output for the sentence above

"GPZDA"
"162913.48"
"19"
"08"
"2005"
"*63"

you see it ignores the ",," empty data field before the "*63" field
i understand this is the case of 2 delimiters in a row with , and this
is the
normal behavior for strtok().
how can i modify my code in order to recognize the empty data field?

i want a way to read this field, so i want the output for the above
sentence
as

"GPZDA"
"162913.48"
"19"
"08"
"2005"
""
"*63"

below is my code

<code>
char s[512];
char seps[] = ",";
char *token;
....
//after reading NMEA sentence from serial port and verifying its
checksum is //correct
if(Is_Sentence_Checksum_Valid(s))
{
printf("%s\r\n",s);
token = strtok(s, seps );
while( token != NULL )
{
//While there are tokens in sentence s
printf( " %s\n", token );
// Get next token:
token = strtok( NULL, seps );
}
strcpy(s,"");
}

</code>

thanks for your help
regards,
 
S

Steve Alpert

hello,
i am writing a GPS app in evc++ 4.0, i am using c-style char* instead
of CStrings, i want to parse NMEA sentence such as

"GPZDA,162913.48,19,08,2005,,*63"
problem is when i use strtok it ignores an empty data field
below is the output for the sentence above

"GPZDA"
"162913.48"
"19"
"08"
"2005"
"*63"

you see it ignores the ",," empty data field before the "*63" field
i understand this is the case of 2 delimiters in a row with , and this
is the
normal behavior for strtok().
how can i modify my code in order to recognize the empty data field?

i want a way to read this field, so i want the output for the above
sentence
as

"GPZDA"
"162913.48"
"19"
"08"
"2005"
""
"*63"

below is my code

<code>
char s[512];
char seps[] = ",";
char *token;
...
//after reading NMEA sentence from serial port and verifying its
checksum is //correct
if(Is_Sentence_Checksum_Valid(s))
{
printf("%s\r\n",s);
token = strtok(s, seps );
while( token != NULL )
{
//While there are tokens in sentence s
printf( " %s\n", token );
// Get next token:
token = strtok( NULL, seps );
}
strcpy(s,"");
}

</code>

As long as you use strtok() it obviously won't work. It's easy enough to roll
your own version that doesn't eat empty fields. Create a function with a couple
of static ptrs to hold you over...

char * mytok(char *str, char *seps) {
static char *base, *p = NULL;
char *ep;
if( str ) base = p = str; // initial conditions
if( !p ) return ""; // never fail
... etc.

}

/steveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top