Alli Alli - 2 months ago 9
C Question

Issue when reading cvs file and storing in buffer

I have a

csv
file of
double
values ( 20 rows and 4 columns) that I want to read and store the values in a buffer to perform some operations. My following implementation gives me some characters on the screen. I tried to see where is the problem but I don't know where:

#include<stdio.h>
#include <stdlib.h>
#include<String.h>


int main()
{
char buff[80];
double buffer[80];
char *token = NULL;

FILE *fp = fopen("dataset.csv","r");
if(fp == NULL){
printf("File Reading ERROR!");
exit(0);
}

int c = 0;
do
{
fgets(buff, 80, fp);
token = strtok(buff,",");

while( token != NULL )
{
buffer[c] = (char) token;
token = strtok(NULL,",");
c++;
}
}while((getc(fp))!=EOF);

for(int i=1; i<=80; ++i){
printf("%c ", buff[i]);
if(i%4 == 0) printf("\n");
}

}


Any help is appreciated.

Answer

Nice attempt, modify it a bit, like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // not String.h

int main(void)
{
    char buff[80];
    double buffer[80] = {0}; // I like initialization my arrays. Could do for 'buff' too
    char *token = NULL;

    FILE *fp = fopen("dataset.csv","r");
    if(fp == NULL){
        printf("File Reading ERROR!");
        exit(0);
    }

    int c = 0;
    while(fgets(buff, 80, fp) != NULL) // do not use 'getc()' to control the loop, use 'fgets()'
    {
        // eat the trailing newline
        buff[strlen(buff) - 1] = '\0';

        token = strtok(buff, ",");

        while( token != NULL )
        {
            // use 'atof()' to parse from string to double
            buffer[c] = atof(token);
            token = strtok(NULL,",");
            c++;
        }
    }

    // print as many numbers as you read, i.e. 'c' - 1
    for(int i=1; i<=c - 1; ++i) // be consistent on where you place opening brackets!
    {
        printf("%f\n", buffer[i]);
    }

    // Usually, we return something at the end of main()
    return 0;
}

Sample run:

C02QT2UBFVH6-lm:~ gsamaras$ cat dataset.csv 
3.13,3.14,3.15,3.16
2.13,2.14,2.15,2.16

C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
3.140000
3.150000
3.160000
2.130000
2.140000
2.150000
2.160000

Notes:

  1. Use atof() to parse from string to double in .
  2. We usually prefer fgets() over getc().
Comments