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
    2

    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,176
    Have you considered using scanf() or sscanf() to parse the lines?

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    2
    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,176
    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.

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