Hey,
I'm wondering if anyone can help with this problem. I'm trying to overwrite a record on a customer.dat file. the code is as follows
You'll notice that i'm bringing in a float which i want to use to replace the Customer.balance value. What i'm getting is garbage been written over in the wrong place.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("C:\\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 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); }
Any ideas
thanks a million



LinkBack URL
About LinkBacks


