944,175 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6029
  • C RSS
Feb 5th, 2005
0

Need Help Reading a csv file created from MSExcel

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alfarata is offline Offline
10 posts
since Feb 2005
Feb 5th, 2005
0

Re: Need Help Reading a csv file created from MSExcel

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!
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 5th, 2005
0

Re: Need Help Reading a csv file created from MSExcel

Quote 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
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 8th, 2005
0

Re: Need Help Reading a csv file created from MSExcel

Thanks for the help Sorry it took me so long to reply.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Alfarata is offline Offline
10 posts
since Feb 2005
Jun 15th, 2010
-1

comma separated file

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
Reputation Points: 9
Solved Threads: 0
Newbie Poster
sinju is offline Offline
1 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need help with pointers and arrays
Next Thread in C Forum Timeline: removing comments in c and c++ program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC