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

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!

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 using fgets 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
*/

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

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

commented: Don't bump old threads -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.