| | |
Need Help Reading a csv file created from MSExcel
![]() |
•
•
Join Date: Feb 2005
Posts: 10
Reputation:
Solved Threads: 0
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
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!
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!
•
•
•
•
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:C Syntax (Toggle Plain Text)
301,36,0.285,2.88,15.000 302,88,0.247,2.88,75.500
#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
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- program for reading from .csv file shows errors (Java)
- reading .csv file and getting the data out of it (Java)
- CSV file (C)
- Reading CSV file into a ADO recordset (ASP.NET)
Other Threads in the C Forum
- Previous Thread: needed recursion help
- Next Thread: How can I change console application font size?
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue number oddnumber odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked repetition research reversing segmentationfault sequential single socket socketprograming standard strchr string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






