Joining these code snippets

This is a discussion on Joining these code snippets within the C Programming forums, part of the General Programming Boards category; Hi, Im doing a project where I make a CSV reader in plain old C. Ive got some great code ...

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

    Joining these code snippets

    Hi,

    Im doing a project where I make a CSV reader in plain old C. Ive got some great code snippets through helpful users on the internet, but after experimenting I have found I have been unable to join the snippets into a working program.

    Basic code structure
    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){
        /* TO DO- Put your code to parse the csv here char by char
         */
        printf("%c",c);
      }
    
      fclose(fp);
      return 0;
    }
    This is my code that stores my CSV file contents and stores them in the memory:
    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);
                      }             
               }
       }
    This is my code that is designed to read the CSV data:
    Code:
    printf( "%s", line );

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    This is how I thought the completed code would look (It doesnt work)
    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;
       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;
    }

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you have 2 while loops one inside another

    first is correct - so the second one should be probably removed as being wrong one
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    More 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;
    }

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    the problem is - the most of your variables declared inside the loop should be initialized outside the loop - like len

    so, obviosly, they should be declared outside the loop as well
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Code Snippets
    By saeculum in forum Linux Programming
    Replies: 1
    Last Post: 03-15-2009, 08:47 PM
  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, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Couple Code Snippets
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 01-22-2002, 01:23 AM

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