Here is a struct I use to write to a random file:
------------------------------------------------------------------------
typedef struct {
int eqNo;
char eqName[20];
float price;
int stock;
int reorder;
} equipment;
------------------------------------------------------------------------
Here is the variable definition:
------------------------------------------------------------------------
equipment Equip = {0,"",0.0,0,0};
------------------------------------------------------------------------
I open the file like this:
------------------------------------------------------------------------
fPtr = fopen(filename, "r+");
------------------------------------------------------------------------
NOTE: filename is a string variable with the actual filename
This prints the data stored in this struct for individual records from the random file just fine:
------------------------------------------------------------------------
printf("1 - Item # : [%d]", Equip.eqNo);
printf("2 - Name : [%s]", Equip.eqName);
printf("3 - Price : [%.2f]", Equip.price);
printf("4 - In Stock : [%d]", Equip.stock);
printf("5 - Reorder : [%d]", Equip.reorder);
------------------------------------------------------------------------
But this, when reading from the random file, prints the first few records, and then gibberish...
------------------------------------------------------------------------
while(!feof(fPtr))
{
fread(&Equip, sizeof(equipment), 1, fPtr);
if (Equip.eqNo != 0)
{
printf("%-7d%-20s%-10.2f%-9d%-8d",Equip.eqNo,
Equip.eqName, Equip.price, Equip.stock, Equip.reorder);
}
}
------------------------------------------------------------------------
The above while loop code works for all of my other structures and random files EXCEPT the structure mentioned above.
So, what am I doing wrong with that loop? Is there something wrong with this loop or is there something wrong with the structure? If you need any more code to help your reasoning, just ask. Thanks in advance.