j0h

parse CSV Character array in C

j0h
Jul 25th, 2021
829
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<string.h>
  2. #include<stdio.h>
  3. #include <stdlib.h> //contians atoi for string to int conversion
  4. /*
  5.  * This program is a simple? way of parsing a csv character array
  6.  *
  7.  *
  8.  * */
  9. int main()
  10. {
  11. int x,y,z;
  12.     char input[16] = "545,394,2";
  13.     char *p;
  14.     p = strtok(input, ",");
  15.     if(p){
  16.      x=atoi(p);
  17.     }
  18.     p = strtok(NULL, ",");
  19.  
  20.     if(p){
  21.     y=atoi(p);
  22.     }
  23.     p = strtok(NULL, ",");
  24.     if(p){
  25.    z=atoi(p);
  26.     }
  27.     printf("X: %d\n", x);
  28.     printf("Y: %d\n", y);
  29.     printf("Z: %d\n", z);
  30.    
  31.     return 0;
  32. }
RAW Paste Data