Convert csv string to float array

Can anyone advise an easy way to convert a csv string to a float array in C?

eg.

char my_string[] = "1.0,2.0,3.0"; 

to:

 my_array = [1.0, 2.0, 3.0] 

where my_array is of type float[]

I would use sscanf as a quick and easy solution, but I do not know how many values ​​are contained in the string in advance

Is there any existing library function that could do this without me, to resort to cycling through each char in search of ","?

+4
source share
6 answers

You can use strtok() :

 float my_array[N]; // if you don't know how many there are, use the heap int i = 0; char *tok = strtok(my_string, ","); while (tok != NULL) { my_array[i++] = atof(tok); tok = strtok(NULL, ","); } 
+6
source

There is a library there that you can use - LibCSV

From their description:

libcsv is a small, simple, and fast CSV library written in pure ANSI C89 that can read and write CSV data. It provides a straightforward interface using callback functions to process parsed fields and strings and can parse incorrectly formatted CSV files.

+3
source

Use a while() that reads only one float at a time with sscanf() . As soon as sscanf() returns 0, you know that you are at the end of the list.

+2
source
 #include <stdio.h> #include <stdlib.h> #include <string.h> char** split(const char *str, const char *delimiter, size_t *len){ char *text, *p, *first, **array; int c; char** ret; *len = 0; text=strdup(str); if(text==NULL) return NULL; for(c=0,p=text;NULL!=(p=strtok(p, delimiter));p=NULL, c++)//count item if(c==0) first=p; //first token top ret=(char**)malloc(sizeof(char*)*c+1);//+1 for NULL if(ret==NULL){ free(text); return NULL; } strcpy(text, str+(first-text));//skip until top token array=ret; for(p=text;NULL!=(p=strtok(p, delimiter));p=NULL){ *array++=strdup(p); } *array=NULL; *len=c; free(text); return ret; } void free4split(char** sa){ char **array=sa; if(sa!=NULL){ while(*sa) free(*sa++);//for string free(array); //for array } } int main(){ char my_string[] = "1.0,2.0,3.0"; float *my_array; char **strs; size_t count; strs=split(my_string, ", \t", &count); my_array=(float*)malloc(sizeof(float)*count); { //convert int i; for(i=0;i<count;++i) my_array[i]=(float)atof(strs[i]); free4split(strs); } { //test print int i; for(i=0;i<count;++i) printf("%f\n", my_array[i]); } return 0; } 
+1
source
 #include <stdio.h> #include <stdlib.h> #include <string.h> char* getToken(char **p, const char delimiter){ char *word, *top=*p; int len; if(*p == NULL || **p == '\0') return NULL; while(**p && **p != delimiter) ++(*p); if(**p != delimiter) return strdup(top); len = *p - top; ++(*p); word=(char*)malloc(sizeof(char)*(len+1)); strncpy(word, top, len); word[len]='\0'; return word; } int main(){ char my_string[] = "1.0,2.0,3.0"; float *my_array=NULL; char *word, *p=my_string; int count=0; while(NULL!=(word=getToken(&p, ','))){ my_array=(float*)realloc(my_array, sizeof(float)*(++count)); my_array[count-1]=(float)atof(word); free(word); } { //test print int i; for(i=0;i<count;++i) printf("%f\n", my_array[i]); } return 0; } 
+1
source

You might want to explore the strchr family of functions.

 STRCHR(3) Linux Programmer Manual STRCHR(3) NAME strchr, strrchr - locate character in string SYNOPSIS #include <string.h> char *strchr(const char *s, int c); char *strrchr(const char *s, int c); DESCRIPTION The strchr() function returns a pointer to the first occurrence of the character c in the string s. The strrchr() function returns a pointer to the last occurrence of the character c in the string s. RETURN VALUE The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found. CONFORMING TO SVID 3, POSIX, BSD 4.3, ISO 9899 SEE ALSO index(3), memchr(3), rindex(3), strpbrk(3), strsep(3), strspn(3), strstr(3), strtok(3) 
0
source

All Articles