arrays - Read a large CSV File and storing the content in C language -
i learning c programming , got stuck 1 code. want read , store large csv file of around 10000 rows , 5 columns, each coloumn having names time, time_diff, sn,rs, fr.
i wrote code it. wanted read different columns, used strtok function read line , store in different variables. read file , print content on screen. not getting how store contents in array.
i tried make array of structures , store values in these array, somehow not working. used random value of int variable i, check if works or not.
can please explain how store different variables in array , how call them back. if want print sn[1000], how store it. following code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define buffer_size 1024*1024 struct filedata { char *time; int time_diff; int sn; int rs; int fr; }; struct filedata data[10000]; int main () { //char buffer [buffer_size]; char *buffer; file *fp; char *token; int filesize; int i; if ((fp=fopen("testfile.csv", "r"))==null) { printf ("file cannot opened"); } buffer = (char*)malloc (buffer_size); if (buffer == 0) { printf("error: out of memory"); return 1; } fgets(buffer, buffer_size, fp); token = strtok(buffer, ";"); while (token !=null) { printf (" \t%s", token); token = strtok (null, ";"); } (i=0; i<4;i++) { while ((fgets(buffer, filesize, fp))) { token = strtok(buffer, ";"); data[i].time = token; token = strtok(null, ";"); data[i].time_diff = token; token = strtok(null, "; "); data[i].sn = atoi(token); token = strtok(null, "; "); data[i].rs = atoi(token); token = strtok(null, "; "); data[i].fr = atoi(token); printf("\t%s\t%s\t%d\t%d\t%d\t\n", data[i].time, data[i].time_diff, data[i].sn, data[i]. rs, data[i]. fr); } } free (buffer); return 0; }
thanks in advance help
your code had several issues. please find corrected version below.
some of issues had:
not saving result of
strtok
infiledata.time
. need copy string, not use assignment. need allocate spacefiledata.time
usingmalloc
(don't forgetfree
eachfiledata.time
allocated in such case) , copy result ofstrtok
there; alternatively can use fixed length strings if like.using wrong format specifier in last
printf
data[i].time_diff
.not using
atoi
time_diff
.use of uninitialized variables. used
filesize
variable without initializing it.struct filedata { char *time; /*increase if or use dynamic memory*/ int time_diff; int sn; int rs; int fr; }; struct filedata data[arr_size]; int main(int argc, char *argv[]) { char *buffer; file *fp; char *token; int filesize = 0; int = 0, j=0; if ((fp=fopen("c:\\test.txt", "r"))==null) { printf ("file cannot opened"); return 1; } buffer = malloc (buffer_size); if (buffer == null) { printf("error: out of memory"); return 1; } fgets(buffer, buffer_size, fp); token = strtok(buffer, ";"); while (token !=null) { printf (" \t%s", token); token = strtok (null, ";"); } while ((fgets(buffer, buffer_size, fp))) { if(i>=arr_size) break; data[i].time=malloc(256); // use constant(or define) more suitable token = strtok(buffer, ";"); strcpy(data[i].time,token); token = strtok(null, ";"); data[i].time_diff = atoi(token); token = strtok(null, "; "); data[i].sn = atoi(token); token = strtok(null, "; "); data[i].rs = atoi(token); token = strtok(null, "; "); data[i].fr = atoi(token); printf("\t%s\t%d\t%d\t%d\t%d\t \n", data[i].time, data[i].time_diff, data[i].sn, data[i].rs, data[i].fr); i++; } // note: don't forget free each data[i].time // allocated space, e.g. for(j =0;j<i; j++) free(data[j].time); free(buffer); return 0; }
Comments
Post a Comment