I'm trying to read a CSV file where I have just double values separated with a comma. I'm using the char *fgets(char *str, int n, FILE *stream)
function to read the rows. In the code, to finish the do-while loop, I'm using the getc()
method to read the next char, to find out if I've read the end of the file. The problem is, getc()
method read the first character from the next line, so I'm losing data (as the first number in the next line loses one digit). As you can see, apart from the first row, all the first column entries lost their first characters.
What should I use to control my while loop? Or is there another method that I should use to read data from CSV files? Thank you very much
Data from my_file.csv:
3.0000,4.0000,5.0000,6.0000,7.0000
6.0000,5.0000,4.0000,3.0000,2.0000
9.0000,6.0000,3.0000,0.0000,-3.0000
12.0000,7.0000,2.0000,-3.0000,-8.0000
15.0000,8.0000,1.0000,-6.0000,-13.0000
18.0000,9.0000,0.0000,-9.0000,-18.0000
Actual output:
[enter image description here][1]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getData(char *buff);
int main() {
char folder_addr[] = "C:\\my_file.csv";
FILE *fp = fopen(folder_addr, "r");
do {
char buff[1024];
fgets(buff, 1024, (FILE*)fp);
printf(buff);
getData(buff);
} while ((getc(fp)) != EOF);
return 0;
};
void getData(char *buff){
char *token = strtok(buff, ",");
//printf("First value: %s\n", token);
while (token != NULL) {
//printf("First value: %s\n", token);
token = strtok(NULL, ",");
}
}
[1]: https://i.stack.imgur.com/fQzw1.jpg