Thread: How to open .csv file and read the entries using C

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    4

    How to open .csv file and read the entries using C

    Hi all,

    I want to open .csv file and read the cell entries in column A and B as shown in the attached image, sample1.jpg and put in the arrays. But the program must be able to detect empty rows (row 29, 32, 34 and 38 as shown in the attached image) for processing other thing.

    I know how to do it for text file. Below is my code
    Code:
    void main(void)
    {
        uint64 array1[1000];
        uint64 array2[1000];  
        uint64 index;
        char line[1000];   
        char *token;
        char *breaks = " \t\n";
        uint64 totalEntries = 0;
       
        uint64 i=0;
        uint64 j=0;    
        
        FILE *fstream = fopen("MCMT10.txt","r");   
        if(fstream == NULL)   
        {      
           printf("\n file opening failed ");      
        } 
    
        while(fgets(line, 1000, fstream) != NULL)
        {
            if(strlen(line)>2)
            {
                token = strtok(line, breaks);
                // skip empty lines
                if (token != NULL) 
                {  
                    if(token != NULL)            
                    {
                      array1[i] = Atoi64(token, 16);
                      i++;
                      totalEntries++;
                    }
                    token = strtok(NULL, breaks);
                    
                    if(token != NULL)
                    {
                      array2[j] = Atoi64(token, 16);
                      j++;
                    }
                }
                
            }
            else
            {
              printf("\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
              i=0; 
              j=0;
              printf("\n*********************************************");
            }
            
        }
        fclose(fstream);
        
        printf("\nTotal entries = %d", totalEntries);
      
    }
    Attached Images Attached Images How to open .csv file and read the entries using C-sample1-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So what's your question?

    main returns int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    4
    How to write C code to open .csv file as shown in the attached image and read the cell entries in column A and B and put in the arrays? But the program must be able to detect empty rows (row 29, 32, 34 and 38 as shown in the attached image) for processing other thing. ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char *breaks = " \t\n";
    Have you considered what effect adding a comma would have?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    CSV = comma separated values

    Hint, hint.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    csv file is an text file; I suggest you find an text editor to look at the formatting of the file.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2013, 09:47 PM
  2. Replies: 2
    Last Post: 12-08-2011, 05:30 AM
  3. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  4. Remote file open & read
    By major9 in forum C Programming
    Replies: 3
    Last Post: 04-24-2009, 04:12 AM
  5. Read/open file
    By HollyMae in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2003, 03:40 AM

Tags for this Thread