Thread: How to parse a csv file in C and put lines with similar pattern together?

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    3

    How to parse a csv file in C and put lines with similar pattern together?

    Hi ,

    I am a newbie in C programming.
    I have got a csv file of this below format to parse and put together lines with similar value under 'syntax' column together.

    my.csv file :

    file,tools,edit,syntax,buffers
    a
    ,b,c,perl,d
    a
    ,w,c33,java,d
    a
    ,e,c,perl,d
    a
    ,s,c,python,d1
    a
    ,n,c,php,d3
    d
    ,r,hhh,cpp,d0
    d
    ,m,hhh,c#,d0
    a
    ,o,c,pdf,d3
    a
    ,f,c,python,dd
    a
    ,h,c,perl,dg
    a
    ,yb,c,c,ddf
    a
    ,b,c,perl,dt
    wa
    ,b,c33,java,d
    d
    ,buuu,hhh,cpp,d0
    d44
    ,b,hhh,c#,d0
    a
    ,be,c,js,d4
    wa,b,c333,java,d
    wa,b,c33,js,dAfter the parsing lines with similar value under syntax should be arranged together i.e all lines with syntax value as 'perl' should be consecutive and so on for other values of syntax like 'java', 'python' etc.

    So far, I have been able to come up with this following code, but not able to proceed further , any help will be much appreciated:

    Thanks.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<fcntl.h>
    #include<string.h>
    
    int main (int argc, char **argv) {
        int line;
        char *inputFile =  argv[1];
        FILE *input_csv_file;
    
         input_csv_file == fopen(inputFile, "w+");
    
         if(input_csv_file ==0) {
            printf("Can not open input file \n");
         } else 
          {
               while((line = fgetc(input_csv_file)) != EOF) {
           
                }
           }
    
      return 0;
    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,203
    Have you considered using scanf() or sscanf() to parse the lines?

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    Have you considered using scanf() or sscanf() to parse the lines?
    No, little demonstration will be very helpful. I know I am being too demanding as I said I am a newbie.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,203
    You can find a "little demonstration" by reading some documentation for the scanf() series of functions. For example this link is the first link in over 3 million that provides documentation for this standard C function.

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    34
    Take your time to learn the basics. Try commenting your programs no mater how small the program may be.

  6. #6
    Registered User
    Join Date
    Jun 2018
    Posts
    3
    Hi,

    I could achieve my requirement with the following piece of code which is working. Please let me know if there is any chance of improvement in it or any other suggestion would be immensely helpful as I am a newbie.



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<fcntl.h>
    #include<string.h>
    
    int main(int argc, char ** argv) 
    {
        struct filedata {
            char pl[8];
            char content[100];
        };
    
        char line[200];
        char *inputFile = argv[1];
        FILE *input_csv_file;
        int iter = 0, c;
    
        char * tok;
        int count = 0;
        char ch;
        char coding_language[] = "syntax";
        char header_line[50];
    
        input_csv_file = fopen(inputFile, "rt");
    
        //count line numbers of the input csv
        for(ch = getc(input_csv_file); ch!= EOF; ch=getc(input_csv_file))
            if(ch == '\n')
                count = count + 1;
    
    
        fclose(input_csv_file);
    
      
        count =  count -1;
    
        struct filedata * record[count];
        input_csv_file = fopen(inputFile, "rt");
    
        if(input_csv_file == 0) 
        {
            printf("Can not open input file\n");
        } else 
        {
            while(fgets(line, sizeof line, input_csv_file) != NULL) 
            {
                //printf("-- line = %s\n", line);
                int s_line = sizeof line;
                char dup_line[s_line];
                strcpy(dup_line, line);
                
                int h = 0;
                int s_token;
    
                tok = strtok(line, ",");
    
                while(tok != NULL) 
                {
                    h++;
                    if(h == 4)
                    {
                        s_token = sizeof tok;
                        break;
                    }
                    tok = strtok(NULL, ",");
                }
    
                char temp[8];
                strcpy(temp,tok);
    
                // skipping the header line
                if(compare_col(tok, coding_language) == 0) {
                    strcpy(header_line, dup_line);
                    continue;
                }
    
                iter++;
                c = iter - 1;
    
                record[c] = (struct filedata*)malloc(sizeof(struct filedata));
                strcpy(record[c]->pl, tok);
                strcpy(record[c]->content, dup_line);
            } //while
        
            struct filedata * temp;
    
            FILE * fptr;
            fptr = fopen("sorted_csv.txt", "w");
            if(fptr == NULL)
            {
                printf("Error in opening the file to write\n");
                exit(1);
            }
    
           // sorting the array of struct to write them as per requirement
            for(iter=1; iter < count; iter++)
                for(c =0 ; c < count -1; c++) {
                    if(strcmp(record[c]->pl, record[c+1]->pl) > 0) {
                        temp = record[c];
                        record[c] = record[c+1];
                        record[c+1] = temp;
                    }
                } 
            
            for(iter=0; iter < count; ++iter) 
            {
               
                if(iter == 0) {
                fprintf(fptr, "%s", header_line);
                    continue;
                }
    
                fprintf(fptr, "%s", record[iter]->content);
            }
        fclose(fptr);
        }
        fclose(input_csv_file);
    }
    
    int compare_col(char a[], char b[] )
    {
        int c = 0;
        while(a[c] == b[c]) {
            if(a[c] == '\0' || b[c] == '\0')
                break;
            c++;
    
        }
    
        if(a[c] == '\0' && b[c] == '\0')
            return 0;
        else 
            return -1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I parse an XML file?
    By AlecTaylor in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2011, 03:19 AM
  2. how to parse this file?
    By zcrself in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2010, 03:51 AM
  3. Replies: 0
    Last Post: 11-19-2008, 01:16 AM
  4. Parse a file
    By L_U_K_E in forum Windows Programming
    Replies: 1
    Last Post: 04-03-2007, 05:20 AM
  5. Parse error in .rc file.
    By Ranedhel in forum Windows Programming
    Replies: 1
    Last Post: 07-29-2003, 02:52 PM

Tags for this Thread