I want to scan a text file:
Code:
jones,1,2,3,4
smith,2,3,4,5
john,57,65,4,2
catherine,25,64,78,65
I want ask a few questions:
(1) Can anybody give me any functions on how I can use to do this (or point me in the general direction).
I tried using fscanf, but it keeps on giving me the wrong answers.
Below is the code
Code:
#include <stdio.h>
#define MAXSTUDENT 40
#define MAXNAME 20
typedef struct student_t{
char name[(MAXNAME+1)];
int exam1,exam2, exam3, exam4;
float average;
};
int main(void)
{
FILE *readfile;
int num=4;
int check;
student_t student[MAXSTUDENT] = {0};
int i;
if((readfile=fopen("datafile.txt","rt"))==NULL)
{
printf("Error, unable to read file. Program exiting");
return (1);
}else
{
for(i=0,check=0;i<num;i++)
{
do
{
check=fscanf(readfile,"%s,%d,%d,%d,%d",&student[i].name,&student[i].exam1,&student[i].exam2,&student[i].exam3,&student[i].exam4);
printf(".");
}while(check==4);
printf("*");
}
printf("\n%d",student[0].exam1);
printf("\n%d",student[0].exam2);
printf("\n%d",student[0].exam3);
printf("\n%d",student[0].exam4);
}
return 0;
}
Thanks in advance.