View Full Version : Char Array manipulation ... help!
LiquidG
09-25-2002, 07:50 PM
I need a quick refresher on some C commands that act on char arrays ... what I have is a GPS unit connected to my machine, and I want to manipulate some of the raw NEMA data ... can anyone help me on what C functions to use for the following:
Assuming that the data from the GPS comes as a char array (or a string, but i'm almost certain a char array), i want to create a data structure that parses the data ... the data is deliminated by field, i.e.:
<location><direction><velocity><etc...>
is what will be passed to me ... now, given that its a char array, how do i extract these specific values? Assume that the location field contains to floats, and that the direction and velocity are doubles.
I know this C functions exist for this, I'm just not familiar with them, thanks!
Strike
09-25-2002, 09:35 PM
atoi and atof maybe?
LiquidG
09-25-2002, 09:54 PM
Thanks folks ... sscanf, atoi, and atof are what i'm looking for, but i still have one question that needs clarification ... the input that I'm getting from the char arra is not deliminated by white space or the like ... its just one field after the next - i.e., if i were at location "boo_foo", was heading "no_where" and traveling at a speed of "fast", the char arrray would look like such:
boo_foono_wherefast
Of course, the types of these three fields are known (as specified in my original post). Now, how do i parse this beast? (again, I know this must be very simple, my C is rather rusty though)
Uh, if there's no delimiter, then the only way you could parse it is if you know the length of each field.
LiquidG
09-25-2002, 10:31 PM
Sorry for not explicitly stating that, but yes, the length of each field is specifically known
Strike
09-25-2002, 10:36 PM
couldn't you just do:
sscanf(foo, "%7s%8s%4s", location, heading, direction);
?
(assuming those field lengths are right)
LiquidG
09-26-2002, 12:42 PM
Last question folks :) ... now, suppose the char array is constantly streamed in as input ... ie, lets say its of fixed length 23 (going with the 7, 8, 4 field lengths and an extra field for time thats of length 4 so as to timestamp the fields) ... is there a function in C that allows for a buffer to be an incoming stream that I can thus parse accordingly (as suggested with sscanf above)? I'd prefer not to append the output of the stream to some growing text file (since it would get large after a while).
I'm confused as to how to after the initial pass of the buffer to sccanf how to position the pointer to that buffer to the next block of data since it is streaming in ... suggestons please? ... and thanks as always!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.