Hello, first post here. Be gentle. Anyway i am trying to read in and print data structures from a binary file. Initially i tried it this way
Code:
while(!feof(fp))
{
if(fread(&data, sizeof(DATA), 1, fp) != 1)
{
fprintf(stdout, "ERROR: file read\n");
exit(1);
}
printrecord(data);
}
Basically this didnt really work, in the sense that it would do what i wanted it to do but at the last record it would trigger the ERROR: file read and then print out the last record again. After doing some searches and reading some of your fine posts i gathered that this was because of my misguided use of !feof(fp) as a stop condition. So i reworked it and came up with:
Code:
while(fread(&data, sizeof(DATA), 1, fp) != NULL)
{
printrecord(data);
}
While this one works (as far as i tested it) in the sense that it prints out all the records and then stops after the last record without any errors, it is more or less devoid of any error checking.
Would you please post your thoughts and let me know the generally accepted method for accomplishing this?
Thank you,
-Emrys