Thread: CSV parsing - Can you look at my code?

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

    CSV parsing - Can you look at my code?

    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:

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

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Mmm...copypasta! Just like your previous thread.

    Well, at least it's nice to see the extensive effort you've put into Lance's work since he so kindly gave this code to you.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Im trying to learn C by making a simple program, ive been reading up on tutorials but its still hard to understand so I need to ask for help. Im sorry if this isnt allowed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM