Need Help Reading a csv file created from MSExcel

Reply

Join Date: Feb 2005
Posts: 10
Reputation: Alfarata is an unknown quantity at this point 
Solved Threads: 0
Alfarata Alfarata is offline Offline
Newbie Poster

Need Help Reading a csv file created from MSExcel

 
0
  #1
Feb 5th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,947
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 914
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Need Help Reading a csv file created from MSExcel

 
0
  #2
Feb 5th, 2005
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!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need Help Reading a csv file created from MSExcel

 
0
  #3
Feb 5th, 2005
Originally Posted by Alfarata
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:
  1. 301,36,0.285,2.88,15.000
  2. 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
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 10
Reputation: Alfarata is an unknown quantity at this point 
Solved Threads: 0
Alfarata Alfarata is offline Offline
Newbie Poster

Re: Need Help Reading a csv file created from MSExcel

 
0
  #4
Feb 8th, 2005
Thanks for the help Sorry it took me so long to reply.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC