Hi,
Ive been trying to learn C programming. The project ive set myself is to make a CSV text file reader and parser. The aim of the program is to read a CSV file, then display it, breaking it down into its seperate values. I then want to be able to call individual pieces of information later on.
My current code looks like this:
The errors I get are attached in the image below. This is my first program, so I would really appreciate a little help working out why these errors are occuring, and also a little insight into if my code is the correct way of going about reading a csv file.Code:#include <stdio.h> int main(int argc, char ** argv){ int c; FILE * fp; if(argc < 2){ printf("Usage:\n\t%s filename\n",argv[0]); return -1; } if((fp = fopen(argv[1],"rb")) == NULL){ printf("can't open %s\n",argv[1]); return -2; } while((c = fgetc(fp)) != EOF){ #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; 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); } } } printf( "%s", line ); } fclose(fp); return 0; }
Regards,
James


