I have a struct of students that written to a file all what i want is intial the array with values from the file using any method , the aim is to put the struct values into array to help sorting process the code gives me punch an kick any idea how to achive it
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
struct student
{
char id[5];
char name[30];
int term;
float gpa;
char grade;
};
struct student stu;
typedef struct student stud;
int main ()
{
FILE *fp;
fp = fopen("record.txt","ab+");
char nextChar = getc(fp);
int numCharacters = 0;
while (nextChar != EOF)
{
numCharacters++;
nextChar = getc(fp);
}
//////// detect number of characters ////////
int chunck = numCharacters/sizeof(stu);
stud *arr = (stud *)malloc(chunck);
/*//problem comes next ...
// intial the array of students with records from file
while(fread(&stu,sizeof(stu),1,fp) == 1)
{
for(d=0;d<chunck;d++)
{
arr[d].id=stu.id;
arr[d].name=stu.name;
arr[d].term=stu.term;
arr[d].gpa=stu.gpa;
}
}*/
return 0;
}