Sunday, June 26, 2011

Read csv file in C

Here is the C code to get csv file data.

-------------------------------------------------------------------------------------
#include "stdio.h"
#include "stdlib.h"
#include "math.h"

int main(void){

FILE *fin;
float a=0;
int nb_data = 60, i=0;

float* ReadArray;
ReadArray = (float *)malloc(sizeof(float)*nb_data);


/* open file */
if( (fin=fopen("TestData.csv","r")) == NULL ){
printf("file cant open\n");
exit(1);
}
/* get data one by one */
while( fscanf(fin,"%f,", &a) != EOF )
{
/* print */
ReadArray[i] = a;
printf( "read value=%f\n", ReadArray[i]);
i++;
}

fclose( fin );
}
-------------------------------------------------------------------------------------

No comments:

Post a Comment