CSV Reader in C programming
$20-25 USD
Posted about 15 years ago
$20-25 USD
Paid on delivery
I have the start of a simple CSV C program, that reads the file, parses it, then displays it. The code is 80% done but there is a litte bit of code that needs to be added to read the file and there may be other errors.
I would like someone to finish the program so it works without errors.
## Deliverables
This is the code I currently have:
#include <stdio.h>
#include "stdafx.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;
? ? 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);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? }
? ? }
? ? ? printf( "%s", line );
? }
? fclose(fp);
? return 0;
}
Project ID: 3631216
About the project
47 proposals
Remote project
Active 15 yrs ago
Looking to make some money?
Benefits of bidding on Freelancer
Set your budget and timeframe
It's free to sign up and bid on jobs
47 freelancers are bidding on average $14 USD for this job
$17 USD in 3 days
4.9 (671 reviews) $21.25 USD in 3 days
4.8 (441 reviews) $21.25 USD in 3 days
4.9 (59 reviews) $20.40 USD in 3 days
5.0 (6 reviews) $21.25 USD in 3 days
5.0 (52 reviews) $17 USD in 3 days
4.8 (26 reviews) $21.25 USD in 3 days
5.0 (28 reviews) $12.75 USD in 3 days
4.6 (70 reviews) $6.80 USD in 3 days
4.9 (125 reviews) $21.25 USD in 3 days
4.7 (33 reviews) $17 USD in 3 days
4.8 (34 reviews) $13.60 USD in 3 days
5.0 (32 reviews) $12.75 USD in 3 days
4.7 (19 reviews) $17 USD in 3 days
5.0 (42 reviews) $12.75 USD in 3 days
5.0 (5 reviews) $20.40 USD in 3 days
4.6 (26 reviews) $21.25 USD in 3 days
5.0 (16 reviews) $20.40 USD in 3 days
4.8 (7 reviews) $21.25 USD in 3 days
3.8 (13 reviews) $12.75 USD in 3 days
4.6 (20 reviews)