473,834 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.4 8,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 1375
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.4 8,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
4997
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
6149
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 (isalpha(*pt)) printf("A character is found\n");
8
4032
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
4979
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 declared char** format = NULL; Now the problem starts. Each time strtok() returns me a token (which is char*), I call malloc() and I (want to) store the address returned in the array format, and copy the token to that address. code snippet:
8
1799
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 iteration. breaks on this line: buffString = new char; The contents of the text file it is reading: 1,1,1,1,1,1,1,1,1,5,6,7,8,9 2,2,2,2,2,2,2,2,2 3,3,3,3,3,3,3,3,3
11
2042
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 trying to do strncpy and such. please help.
10
11342
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
5066
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() function. However, the .c_str() string class function returns type const char*. The relevant code may be found below. My compiler returns the error "invalid conversion from `const char*' to `char*'".
4
2377
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
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10504
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7755
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.