thanks for getting back Salem, i've tried your suggestions and i adapted the code
Code:
int updatebal(float bal){
struct Customer_rec // Set up the structure
{
long id;
char name[25];
float balance;
};
struct Customer_rec Customer;
FILE *ifp;// file pointers
if ((ifp=fopen("CUSTOMER.DAT","r+")) == NULL){
printf("Can't open file.");
fclose(ifp);
return(-1);
}
//Trying to find the correct start pt.
fseek(ifp,sizeof(struct Customer_rec)*3,SEEK_SET);
fread(&Customer,sizeof(struct Customer_rec),1,ifp);
//Change value for the balance
Customer.balance = bal;
fseek(ifp,sizeof(struct Customer_rec)*3,SEEK_SET);
//Write it back to the file
fwrite(&Customer,sizeof(struct Customer_rec),1,ifp);
//Print the updated balance
//fflush(ifp);
fseek(ifp,sizeof(struct Customer_rec)*3,SEEK_SET);
if((fread(&Customer,sizeof(struct Customer_rec),1,ifp)) != 1)
printf("\n Error in reading file");
else
printf("\n%ld %s %7.2f\n",Customer.id,Customer.name,Customer.balance);
fclose(ifp);
}
I inputted 15 for the id and 15 for the new balance.
I commented out the flush and i got this as the output written to Customer.dat file:
ID NAME BALANCE
12343 F.Jones 0.00
1432 P.Kelly 150.50
15011 L.Byrne @0.00
15 P.Curran 850.00
1234 O.Lynch 1092.00
the orignal value for L.Byrne's balance was 1000.00
I have to be honest i'm not really that clued in to fread / fwrite and fseek so the '*3' i put in fseek is because the amount of members in my structure is that correct? I'm thinking now it might have something to do with the result i got. Plus it seems that it won't recognize the value?