Beginner C programmer - My CSV reader

This is a discussion on Beginner C programmer - My CSV reader within the C Programming forums, part of the General Programming Boards category; Hi, Ive been trying to learn C programming. The project ive set myself is to make a CSV text file ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Beginner C programmer - My CSV reader

    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:
    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;
    }
    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 Images Attached Images  

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Most of the errors are because of missing #includes.
    You have to include
    Code:
    #include <stdlib.h>
    #include <string.h>
    remove that line
    Code:
    .....
    and there is a closing '}' too many. Proper indentation helps to find such errors.
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi,

    Thank you very much for your help. I did the steps you told me, I deleted the ...., I added the headers and finally I deleted one of the } at the end, leaving me with two instead of three. This then gave me two remaining errors shown in the attached picture. The first is a warning, so I guess that wouldnt stop the program working. It says the add_a_record identifier is not found. What exactly does it mean by this, am i meant to define this somehow?
    Attached Images Attached Images  

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    you don't have a function "add_a_record".

    The warning about fopen is very strange since fopen_s is non-standard, so your compiler is apparently telling you that it is more important than C.

    If you are reading a text file, you should use "r" and not "rb" in the fopen().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    I got help on the forums with that bit of code so i dont really understand how I need to define add_a_record. What should the function add_a_record be doing?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by spadez View Post
    I got help on the forums with that bit of code so i dont really understand how I need to define add_a_record. What should the function add_a_record be doing?
    why do you call this function if you do not even know what it should do?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    That is a piece of code I got help with, im currently trying to intergrate it and understand why it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner c++ programmer compile errors
    By dodo10 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2008, 04:37 PM
  2. [C] - String Manipulation {Beginner Programmer}
    By INFERNO2K in forum C Programming
    Replies: 14
    Last Post: 05-21-2005, 11:34 AM
  3. Beginner Programmer
    By silverjump in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 06:03 PM
  4. Beginner c++ programmer looking for suggestions....
    By Sheshi in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 03:38 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21