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
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:
if(fread(&data, sizeof(DATA), 1, fp) != 1)
fprintf(stdout, "ERROR: file read\n");
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.
while(fread(&data, sizeof(DATA), 1, fp) != NULL)
Would you please post your thoughts and let me know the generally accepted method for accomplishing this?