Parsing a CSV file in C -
i have following code below:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int lendata; printf("content-type:text/html\n\n"); printf("<html><body>"); lendata = atoi(getenv("content_length")); char *buf = malloc(lendata+1); int i=0; char *data; while((data=fgets(buf,lendata+1,stdin)) != null){ char *lines[i]; lines[i] = strdup(data); printf("%s<br>",lines[i]); i++; } printf("%d, %d",lendata,i); free(buf); printf("</body></html>"); return 0; }
i trying parse *.csv file different data types inside (i.e. character strings, intger ). how can process each line in file? thanks!
maybe better....
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int lendata; printf("content-type:text/html\n\n"); printf("<html><body>"); lendata = atoi(getenv("content_length")); char *buf = malloc(lendata+1); int i=0; char *data; while((data=fgets(buf,lendata+1,stdin)) != null){ /*char *lines[i]; */ /* lines[i] = strdup(data); */ printf("%s<br>",data); i++; } printf("%d, %d",lendata,i); free(buf); printf("</body></html>"); return 0; }
sscanf call returns number of entries matched.
numentries = sscanf(line,"%s,%s,%s,%s,%s",ent1,ent2,ent3,ent4,ent5);
Comments
Post a Comment