Overwrting a record

This is a discussion on Overwrting a record within the C Programming forums, part of the General Programming Boards category; Hey, I'm wondering if anyone can help with this problem. I'm trying to overwrite a record on a customer.dat file. ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Overwrting a record

    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
    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);			
    }
    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.
    Any ideas

    thanks a million

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > //Print the updated balance
    Two things you need to do
    1. fflush(ifp);
    This ensures that the file is up to date - especially important if you're mixing reads and writes

    2. fseek(ifp,sizeof(struct Customer_rec)*3,SEEK_SET);
    Reposition the pointer to the start of the record you just wrote.

    Checking the status results of the functions would be a good idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    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?
    Last edited by mossy; 03-16-2004 at 05:01 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    The 3 is the record number, starting at 0

    Imagine the file as
    struct Customer_rec Customers[10];

    With a real array, you just do things like
    Customers[3].balance = 1234.5;

    With a file, you use fseek to locate the record you want
    fseek(ifp,sizeof(struct Customer_rec)*3,SEEK_SET);
    then you read/modify/write the record you want.

    Put the fflush() back in and try again. You're going to see odd things if you don't do that between fwrite() and fread()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    I put the flush back in and i'm getting the same results. Its putting garbage in, in the wrong place on file.

    ie The orginal was '1000.00'

    after i insert '45' it becomes '4@0.00' on file and '45.00' on screen. So it writes it correctly to screen but not to file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. behind and confused
    By steviecrawf in forum C Programming
    Replies: 1
    Last Post: 11-09-2001, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21