I am quite new to C and right now I try to read my first CSV files. But somehow I just cant read the MS-DOS formatted files. On top of that (and what confuses me the most) is, that my program reads the Macintosh-formatted files with only CR on the end just fine, while my code contains CR LR (/r /n). But this wont work for the MS-DOS files, which exactly includes CR LF. What am I missing? It a
Tried to change /r /n to /r and /n. Nothing happend for both formats, Macintosh was read fine, while MS-Dos wont. It always stops after the first line of the CSV file (the row "titles") and the crashes with return value -1.073.741.819 for the MS-DOS format.
My CSV:
Category1;Category2;Category3;Category4;;
Snabb;23;1;34;;
Snibb;32;2;15;;
Snobb;12;3;9;;
My Code:
int calculator()
{
FILE *fp;
char data [1500];
fp = fopen("data.csv","r");
char *token;
int nummer = 1;
int counterCat = 1; //counter-values for reading the first line (the categories)
int maxCat = 4;
float buffer_tf = 0;
float buffer_mf = 0; // buffers for the values the are read-in
float buffer_na= 0;
float buffer_dh = 0;
fgets(data,1500,fp);
printf("\n\n All data:%s",data); // show all data
token = strtok(data,";"); // set the first token
printf("\n%i. %s",nummer,token);// print the first category of the CSV table
while (counterCat != maxCat){ //print all left categories of the CSV table
token = strtok(NULL,";");
nummer ++;
printf("\n%i. %s",nummer, token);
counterCat++;
}
token = strtok(NULL,"\r\n"); //jump to the next token where the data lies
token = strtok(NULL,"\r\n");
nummer = 1;
while (feof(fp) != true){
if (token != NULL)
{
printf("\n\n\n starting run");
printf("\n\n%i. dataset:\n\n",nummer); //dataset counter
nummer ++;
token = strtok(NULL,";"); //read first row of data
printf("%s\n",token);
token = strtok(NULL,";"); //read second row of data
printf("%s\n",token);
buffer_tf = atof(token);
printf("bt=%f\n",buffer_tf);
token = strtok(NULL,";"); //read third row of data
buffer_na = atof(token);
printf("%s\n",token);
printf("bt=%f\n",buffer_na);
token = strtok(NULL,";"); //read fourth row of data
buffer_dh = atof(token);
printf("bt=%f\n",buffer_dh);
token = strtok(NULL,"\r\n");/ /jump to the next row for the next run
printf("\n\n run end");
}
}
return 0;
}