954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help Reading a csv file created from MSExcel

I need to read in a csv file created by MSExcel into an array. The file contains 750 rows of 5 sets of numbers some int. some float. the format looks like this: 301,36,0.285,2.88,15.000
302,88,0.247,2.88,75.500

How do I read this data in a 'C' program without the commas, and then rewrite it with changes that will be made programically.

Thanks in advance
Alfarata

Alfarata
Newbie Poster
10 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

At first glance use the string function strtok() to build a two-dimensional array of numeric strings signifying row and column. Convert the strings without a period to integer values and the ones with a period to float on the fly as you do your math using atoi() and atof().

Now this will make hair grow on your teeth, but you have to build a second array for the math results or changes, then rebuild your csv file format from that. Use all the power of your brain!

vegaseat
DaniWeb's Hypocrite
Moderator
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I need to read in a csv file created by MSExcel into an array. The file contains 750 rows of 5 sets of numbers some int. some float. the format looks like this:

301,36,0.285,2.88,15.000 
302,88,0.247,2.88,75.500

To me it sounds like you'd want to read a line as a string usingfgets and then scan this formatted string with sscanf. Something like this?

#include <stdio.h>

struct record
{
   int     a, b;
   double  c, d, e;
};

int main(void)
{
   const char filename[] = "file.csv";
   FILE *file = fopen(filename, "r");
   if ( file != NULL )
   {
      char line [ 80 ];
      struct record record [ 750 ];
      size_t count, i = 0;
      while ( i < sizeof record / sizeof *record )
      {
         if ( fgets(line, sizeof line, file) == NULL )
         {
            break;
         }
         if ( sscanf(line, "%d,%d,%lf,%lf,%lf", &record[i].a, &record[i].b,
                     &record[i].c, &record[i].d, &record[i].e) == 5 )
         {
            ++i;
         }
      }
      fclose(file);
      for ( count = i, i = 0; i < count; ++i )
      {
         printf("record[%lu]: a = %d, b = %d, c = %g, d = %g, e = %g\n", 
                (long unsigned)i, record[i].a, record[i].b, record[i].c, 
                record[i].d, record[i].e);
      }
   }
   else
   {
      perror(filename);
   }
   return 0;
}

/* file.csv
301,36,0.285,2.88,15.000
302,88,0.247,2.88,75.500
*/

/* my output
record[0]: a = 301, b = 36, c = 0.285, d = 2.88, e = 15
record[1]: a = 302, b = 88, c = 0.247, d = 2.88, e = 75.5
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanks for the help Sorry it took me so long to reply.

Alfarata
Newbie Poster
10 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

i need to comma separated file in c program how to do it
for example
input 10000000000
i need output should be 1,00,000,0000,0
thanks in advance

sinju
Newbie Poster
1 post since Jun 2010
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You