Originally Posted by
Salnitro
ah now i seee, thanks for the heads up.
Its odd because the lecturer told us it was ok to use the gets function.
whats really bugging me is that when i try and change a single field it writes the last record 42 times.
Your lecturer is obviously not particularly concerned with writing code that is robust and safe.
Code:
for(i=1;i<=42;i++)
{
fseek(main_program,(i-1)*sizeof(Student),SEEK_SET);
fread(&slot,sizeof(Student),1,main_program);
if(strcmp(slot.id, "")>0)
{
fprintf( write_file, "%-8s%-9s%-15d%10d%8c\n", slot.name, slot.id, slot.mark1,slot.mark2, slot.grade);
}/*end if*/
}/*end while*/
This will do 42 steps through the loop, no matter how much data is in the file, since you don't check if fread() actually did read something or not. If it fails to read, it won't update the content of slot, so it will repeat the same thing over and over. ]
Checking the return value from functions is another thing that should be done to make sure the code is correct [and of course, coupled with appropriate action when error conditions are detected - just checking it and not doing something about it is pretty pointless].
--
Mats