Hi,
Can someone please look at my code for a C programming project. The idea is to read the contents of a CSV file, store the information and then call it when needed for calculations. The program must be C and not branch into C++ or C#.
Here is the code:
Code:
#define MAX_LINE_LEN 1024*512 /* 1/2 mega byte, should be more than sufficient */
.....
char line[MAX_LINE_LEN];
int len=0;
int cnt_of_fields=0;
char *p;
while( (c=GET_A_CHAR_FROM_FILE(fp))!=EOF){
switch(c)
{
case '"':
case '\'':
/* parse a quoted string, ignore it for now */
break;
case ',':
++cnt_of_fields; /* a comma signal end of previous field and begining of next fields */
line[len++]='\0';
if(len==MAX_LINE_LEN){
fprintf(stderr, "Line too long\n");
exit(-1);
}
break;
case '\n':
++cnt_of_fields; /* a EOL is end of record, and at the same time end of field */
line[len++]='\0';
/* make a copy of the line in the heap, note strdup or strcpy won't work in our case */
p = (char *)malloc(len);
memcpy(p, line, len); /* now all the fields in the record are stored in p[ ] */
add_a_record( p );
break;
default:
line[len++] = c;
if(len==MAX_LINE_LEN){
fprintf(stderr, "Line too long\n");
exit(-1);
}
}
}
There are two things im a little confused about though. How do I call the information into the prgram from the memory? Someone told me to research linked listings but I dont really know how to intergrate it into the coding.