PDA

View Full Version : CSV parsing - How to call data?


spadez
02-05-2009, 12:09 PM
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:

#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 program from the memory? Someone told me to research linked listings but I dont really know how to intergrate it into the coding.

oesxyl
02-05-2009, 01:02 PM
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:

#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 program from the memory? Someone told me to research linked listings but I dont really know how to intergrate it into the coding.
MAX_LINE_LEN is 1/2 kilo, not mega bytes.
Data in csv is tabular so you can use a linked list structure or a array with two index.

This is a homework assignment? You have any problem with my suggestions from other thread of yours about this problem?

best regards

spadez
02-05-2009, 01:49 PM
Hi,

No its not homework, im trying to learn C by hands on practice. I didnt understand your other reply im afraid, as im new to this and I didnt get the connection.

oesxyl
02-05-2009, 01:58 PM
Hi,

No its not homework, im trying to learn C by hands on practice. I didnt understand your other reply im afraid, as im new to this and I didnt get the connection.
about FSM or about how to implement it? It's not a problem and is not so complicate, just let me know what you don't understand.

best regards

flynch01
02-05-2009, 03:42 PM
MAX_LINE_LEN is 1/2 kilo, not mega bytes.

1024 bytes * 512 bytes is 524'288 bytes, which is 512KB. Which, is exactly half a Megabyte, not sure how you figured it out as being half a Kilobyte.

Spadez, could you describe a little clearer exactly what it is you mean by call it into the program from memory?

spadez
02-05-2009, 10:04 PM
Hi,

Yes sorry im not being clear. From what I understand of the program so far, the code reads the CSV file, parses it and then stores the information in the memory. I then thought that if I wanted to display the values I had to pull them from the memory. Is this correct?

If so, Im unsure of the code to take the values from the memory and display them.

flynch01
02-05-2009, 11:46 PM
Yes except you don't take values from memory, you just access them. If you look here:


line[len++] = c;

Very simply, each character read from the file is stored in the next memory position of line. So each loop around the memory ends up like this:

Mem 0 : H
Mem 1 : e
Mem 2 : l
Mem 3 : l
Mem 4 : < - line[len++] = c;
Mem 5 : 0
Mem 6 : 0

len++ is simply incrementing the next memory location and sticking the character there. If you want the character back, you just use the same variable. So for example to output the line so far:


printf( "%s", line );


printf reads from line and outputs it to a console.