hello all
anybody could please tell me how to change this code to make it read from a file that has unknown no of students in it. now this part of the programe read only 5 students.
could you please help me to solve this problem as soon as you can.
thanks alot. :sad: :sad:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student {
char name[25];
int marks[5];
};
void main()
{
FILE *fp;
struct student s[5];
int i,j,u;
double ave;
fp=fopen("c:\\test.txt","r");
if(fp==0)
{
printf("couldn't open test.txt\n");
exit(-1);
}
for(j=0;j<5;j++)
{
fgets(s[j].name, 30, fp);
if ( s[j].name[0] != '\0' )// string not empty
s[j].name[ strlen(s[j].name) - 1 ] = '\0';// remove trailing newline (j.w.)
printf("student name is %s\n\n", s[j].name);
for(i=0;i<5;i++)
{
fscanf(fp, "%d\n", &s[j].marks[i] );
printf(" mark %d\n\n", s[j].marks[i]);
}
}
//printf(" Student Name \t Average Mark\t Classification\n\n");
//for(u=0;u<5;u++)
//{
// ave= findmean(s[u].marks,5);
// printf(" %s \t %.0f\t\t\t",s[u].name, ave);
//}
fclose(fp);
}