C++ "sscanf" skip values in CSV file...

Soldato
Joined
10 Apr 2004
Posts
13,497
Ok, been trying with no luck to do this for a while now...:

File line format:
$DATA,123550.000,8108.0053,N,1007.000,....

Code is:
sscanf(string1, "%[^','],%f,%f,%n,%f",&nmea_ident,&time[ii],&latitude[ii],&longditude[ii]);

Gives me:
$DATA, 123550.000, 8108.0053, 0.000

Now its trying to scan in the "N" between the latitude and longditude, i've searched and tried many different things with no luck.

I want it to ignore the N and import the value after it into the longitude array.

Anybody tell me how I tell it to ignore stuff between commas?

Cheers!

Edit:

Ok I've cheated and just added a char variable to swallow it, bodge but hey it works until you lot think of something better :P

In addition to the above:

I need to split "123550.00" which is 12:35:55 so hours:mins:seconds.

Is there a way of doing that when reading from the string to the variables or should I read it in as a string and do something else with it?

If you could just tell me the commands I need with a hint or two that would be great (its university work so I'd rather fiddle myself instead of getting others to do it!)

Cheers
 
Last edited:
Can you not just %c where the N should be, and just place it into a dummy variable...

So to ammend your example:

char dummy;

sscanf(string1, "%[^','],%f,%f,%c,%f",&nmea_ident,&time[ii],&dummychar,&latitude[ii],&longditude[ii]);
 
Can you not just %c where the N should be, and just place it into a dummy variable...

So to ammend your example:

char dummy;

sscanf(string1, "%[^','],%f,%f,%c,%f",&nmea_ident,&time[ii],&dummychar,&latitude[ii],&longditude[ii]);

Thats what i've done. Seems abit of a bodge to me when there is always the possibility of data in strings thats not required!

Any help on the 2nd point?
 
Nope, im afraid its got to be C!

Edit: Cracked it!

Although a weird error now:

$GPGGA, 19:07:57, Lat: 340 Degrees, 9.2760 Seconds, Long: 118 Degrees, 17.5749 Seconds
$GPGGA, 19:07:58, Lat: 340 Degrees, 9.2789 Seconds, Long: 118 Degrees, 17.5836 Seconds
$GPGGA, 19:07:59, Lat: 340 Degrees, 9.2816 Seconds, Long: 118 Degrees, 17.5919 Seconds
$GPGGA, 19:00:800, Lat: 340 Degrees, 9.2839 Seconds, Long: 118 Degrees, 17.5998 Seconds
...
$GPGGA, 19:00:959, Lat: 340 Degrees, 9.3099 Seconds, Long: 118 Degrees, 18.4831 Seconds
$GPGGA, 19:10: 0, Lat: 340 Degrees, 9.3078 Seconds, Long: 118 Degrees, 18.4910 Seconds
$GPGGA, 19:10: 1, Lat: 340 Degrees, 9.3057 Seconds, Long: 118 Degrees, 18.4989 Seconds
$GPGGA, 19:10: 2, Lat: 340 Degrees, 9.3036 Seconds, Long: 118 Degrees, 18.5067 Seconds

Code:
READ: sscanf(string1, "%[^','],%2i%2i%f,%3i%f,%c,%3i%f",&nmea_ident,&time_hours[ii],&time_mins[ii],&time_seconds[ii],&latitude_degrees[ii],&latitude_seconds[ii],&skipvalue,&longditude_degrees[ii],&longditude_seconds[ii]);
WRITE: printf("%s, %1.2i:%1.2i:%2.0f, Lat: %i Degrees, %2.4f Seconds, Long: %i Degrees, %2.4f Seconds\n",nmea_ident,time_hours[jj],time_mins[jj],time_seconds[jj],latitude_degrees[jj],latitude_seconds[jj],longditude_degrees[jj],longditude_seconds[jj]);


Whats going on here? :confused:

Edit 2: PS it *ONLY* happens between 19:08 and 19:10, no where else!

Edit 3: Convert it to a float and allllll is fine.

Oh well, thanks guys :)
 
Last edited:
In addition to the above:

I need to split "123550.00" which is 12:35:55 so hours:mins:seconds.

Is there a way of doing that when reading from the string to the variables or should I read it in as a string and do something else with it?

If you could just tell me the commands I need with a hint or two that would be great (its university work so I'd rather fiddle myself instead of getting others to do it!)

Cheers

If I was doing that, and I assume this is a once off kind of operation, I'd just create that string as an std::string (or wstring, whatever)

and then use std::string::substr to pick out the individual pieces. Then use atoi to cast to int. It's not pretty, but it'd get the job done....
 
Back
Top Bottom