Jump to content

My first program - CSV Reader

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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:

#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;

}


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.

Regards,

James

Attached Files



#2
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
I found the source of some of the errors, including missing headers and an extra "}".

However im still stuck with the 2 errors show attached below. I dont understand what I need to do to fix it, does the add_a_record need to be defined somehow?

Attached Files



#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Since add_a_record appears to be a function, you must define and declare it.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I agree, you call it:

add_a_record( p );

but never declare it.

#5
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
This is the part of the code I got help with, can anyone explain what I need to do in order to get it working?