473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

paring char* using strtok

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,

Nov 17 '05 #1
1 1339
ar*******@yahoo.com wrote:
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

--
Steve Alpert
my email Fgrir_Nycreg @ vqk.pbz is encrypted with ROT13 (www.rot13.org) and spaces

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: tvn007 | last post by:
Hi, I tried to convert string data type to char type so that I can use strtok. I have used c_str(). However, still getting error message when compile . Any help would be appreciate.
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
8
by: Franklin Li | last post by:
Hi all, I have one simple program: #define __EXTENSIONS__ #include <stdio.h> #include <string.h> int main() { char *buf="5/90/45"; char *token;
10
by: yogeshmk | last post by:
I need to break a longer string (with strtok()) and store the smaller strings in an array. since the number of small strings is not fixed/known, I cannot use a declaration like char *format, so I...
8
by: machikelxol | last post by:
I'm having a strange error when I try reading from a file. here is the code: buffstring values = 1, 2, 3, 4 and then it crashes afterwards on the 4th iteration. This works fine until the fourth...
11
by: crrrystal | last post by:
i can't seem to figure out why this code refuses to work: char *s = "hello"; printf("%s", s); i keep getting bus errors when i run this. whyyyyyy? i also get the same type of errors when...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
4
by: Alan | last post by:
How do I convert the type const char* to type char*? I have an input string ("input_string") of class string. I need to convert this to type char* in order to use it in a call to the strtok()...
4
by: ...vagrahb | last post by:
Hi, I have the following structure struct Format { char x; unsigned char a; unsigned char b; unsigned char c; char y;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.