SuMere

NMEA SAMPLE

Sep 21st, 2020 (edited)
1,589
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char *strtok_fr (char *s, char delim, char **save_ptr)
  6. {
  7.     char *tail;
  8.     char c;
  9.  
  10.     if (s == NULL) {
  11.         s = *save_ptr;
  12.     }
  13.     tail = s;
  14.     if ((c = *tail) == '\0') {
  15.         s = NULL;
  16.     }
  17.     else {
  18.         do {
  19.             if (c == delim) {
  20.                 *tail++ = '\0';
  21.                 break;
  22.            }
  23.         }while ((c = *++tail) != '\0');
  24.     }
  25.     *save_ptr = tail;
  26.     return s;
  27. }
  28.  
  29. char *strtok_f (char *s, char delim)
  30. {
  31.     static char *save_ptr;
  32.  
  33.     return strtok_fr (s, delim, &save_ptr);
  34. }
  35.  
  36. int main() {
  37.    int counter = 0;
  38.    char string[100] = "1,1,20200921214503.000,43.324205,11.327886,342.100,0.00,227.8,1,,2.8,2.9,0.9,,19,4,3,,37,,";
  39.    // Extract the first token
  40.    char * token = strtok_f(string, ',');
  41.    char **NMEA = NULL;
  42.  
  43.    NMEA = malloc(21*sizeof(char*));
  44.  
  45.    while( token != NULL ) {
  46.       printf( " %s\n", token ); //printing each token
  47.       NMEA[counter] = token;
  48.       token = strtok_f(NULL, ',');
  49.       counter++;
  50.    }
  51.  
  52.    for (int i = 0; i <= counter; i++) {
  53.       printf("[%d] - %s\n", i, NMEA[i]);
  54.    }
  55.  
  56.    return 0;
  57. }
RAW Paste Data