C : Reading CSV text file with scanf -
i trying read values off csv file using scanf() programme writing. here how doing :
int i, j, id, time1, time2, time3, type, value; scanf("%d,%d,%d,%d,%d,%d", &id, &time1, &time2, &time3, &type, &value); printf("%d", value);
for illustration lets @ csv line 5, 14:09:01, 1, 1013
.
scanf()
reads in id
, time1
correctly, after getting weird values other variables. example, value
showing 32767
, time2
showing 4195536
. know doing wrong?
p.s. : there anyway instead of using time1, time2, , time3 time, print string avoid variables?
file x.csv contains 1 line:
5, 14:09:01, 1, 1013
this trivial code job - time read string char array. note read comma, you'll need rid of it.
#include <stdio.h> #include <string.h> int main(void) { int id, type, value; char time[100]; file* f = fopen("x.csv", "r"); fscanf(f, "%d, %s %d, %d", &id, time, &type, &value); size_t len = strlen(time); time[len-1] = '\0'; // rid of comma printf("%d %s %d %d\n", id, time, type, value); fclose(f); homecoming 0; }
output:
5 14:09:01 1 1013
c csv scanf
No comments:
Post a Comment