I cannot save information of students from file to struct, When i tried to printf my sturct its not working, I think it should work but i dont understand, why it is not working. when i did only struct or use file like this it works.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//John exper 224(number) 99(grade)
//Alex King 199 36
struct Student{
char name[100],surname[100],number[100];
int grade[100];
};
readStudentsFromFile();
printStudents();
rankStudents();
calculateClassAverage();
int main (){
struct Student var[100];
double co;
readStudentsFromFile(&co);
}
readStudentsFromFile(double *co){
char fileName[100],c;
struct Student var[100];
int i,count;
printf("Enter file name: ");
gets(fileName);
FILE *file;
file=fopen(fileName,"r");
if (file){
count=1;
while(!feof(file))
{
c = fgetc(file);
if(c == 'n')
{
count++;
}
}
for(i=0;i<count;i++){
fscanf("%s %s %s %d",&var[i].name,&var[i].surname,&var[i].number,&var[i].grade);
}
}else{
printf("Error: Unable to open file %s",fileName);
}
*co=count;
printf("%s",var[1].name);
fclose(file);
}
i need to save information to sturct
New contributor
Talha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.