The code below takes in program scores for an array of structures containing student information. It will correctly take in program scores for the first student in the array and write them to the file. For any other students, it will ask for the grades and accept them with no errors, but will not write them to the file.
Any ideas why?
Thanks
Code:
void addProg()
{
int fd,s;
struct rec *stdnt;
fd = open("stdnt.dat", O_RDONLY, 0666);
s = lseek(fd,0,L_XTND);
int n = s/sizeof(struct rec);
lseek(fd,0,L_SET);
stdnt = malloc(s);
read(fd,stdnt,s);
int progNum;
printf("Enter Program Number: ");
scanf("%d", &progNum);
int i;
int tempProgScore;
close(fd);
fd = open("stdnt.dat", O_WRONLY, 0777);
printf("%d",n);
for(i=0;i<n;i++)
{
printf("Enter %s' score for program #%d:",stdnt[i].name, progNum);
scanf("%d",&tempProgScore);
stdnt[i].prog[progNum-1]=tempProgScore;
}
write(fd,stdnt,sizeof(struct rec));
close(fd);
}