I bet the answer to my problem is hidden somewhere in these forums as this seems to be a fairly common question, yet after literally 2 days of searching for an answer I can't find it so please bare with me.
What I'm trying to do here is to read a .csv file, 200 rows, 2 columns, standard formation, comma separated. Then I want to store the contents of the file in an array and be able to manipulate data from that point on. This is the code I've come up with so far.
#include "stdafx.h"
#include "conio.h."
#include "stdlib.h"
#include "string.h"
int _tmain(int argc, _TCHAR* argv[])
{
struct
{
char grade[10];
char name[15];
} student[200];
FILE *fp;
char count[200];
int i=0,j;
fp=fopen("students.csv","r");
if(fp==NULL)
{
printf("Could not properly load file!\n");
getch();
exit(8);
}
else
{
printf("File openned and loaded properly\n\n");
}
while(fgets(count,20,fp)!=NULL)
{
strcpy(student[i].grade,strtok(count,","));
strcpy(student[i].name,strtok(count,","));
++i;
}
for(j=0;j<200;j++)
{
printf("%s\t",&student[j]);
}
printf("\nClosing file\n");
fclose (fp);
getch();
return 0;
}
The csv file looks pretty much like this:
15.1,John
17.34,Angella
23.31,Jake
82.49,Christy
I know my problem should pretty much be in line 34. At the moment the program does compile and properly outputs the data contained on the first column which is numerical (float). I just have no clue how to parse second column data (the names) into student[].name
Assuming I get to the point that data is properly separated in student[].grade and student[].name I'd like to convert the numerical data from char strings into float numerical data. I understand atof is the way to do but after experimenting with it I can't get it to work, getting strange results no matter how much I tried.
Thanks in advance, please ignore the fact that my code most likely sucks, I'm a complete newbie when it comes to C and programming in general. Also please note that C++ is not an option, only ANSI C is allowed.