Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fread problem

Status
Not open for further replies.

DerPflug

Programmer
Mar 28, 2002
153
US
I am attempting to read a binary file to my screen. It loads fine but prints the last record twice. I've exhausted many possibilities but apparently not the right one! Below is the code I'm using. Thanks for the help.

inp = fopen(file_name, "rb");
do
{
fread(&partrecord, sizeof(part), 1, inp);
printf("%s %-20s %.2f %-d %-d %d
%d/%d/%d\n",
partrecord.part_id, partrecord.part_descr,
partrecord.unit_price, partrecord.quan_onhand,
partrecord.reodr_pt,partrecord.reodr_qty,
partrecord.reodr_date.month,
partrecord.reodr_date.day,
partrecord.reodr_date.year);
}while (!feof(inp));
fclose(inp);
 
try...
do
{
if(fread(&partrecord, sizeof(part), 1, inp) < 1)
{
if(feof(inp) != 0)
{
fclose(inp);
break;
}
perror(&quot;fread&quot;);
... we may have been interrupted
... and may have some data in the buffer
fclose(inp);
break;
}
... do your printing
}while(1)
 
OR use

while((fread(&partrecord, sizeof(part), 1, inp))!=1){
printf(&quot;%s %-20s %.2f %-d %-d %d
%d/%d/%d\n&quot;,
partrecord.part_id, partrecord.part_descr,
partrecord.unit_price, partrecord.quan_onhand,
partrecord.reodr_pt,partrecord.reodr_qty,
partrecord.reodr_date.month,
partrecord.reodr_date.day,
partrecord.reodr_date.year);
} Hoping to get certified..in C programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top