title: C Overwrite existing record in file
body: <p>I'm writing a program that has a struct for bank clients (BankAccount) having 3 elements - account number, name of client, and bank balance. I have a file with records already in it, and I have a function to modify the balance. However, I am unable to do so. The balance remains the same. I think my fseek() is wrong, but I'm not sure how/why. I've used fflush(stdin) in some places where it may not be required, but I don't think that's related to the problem. I've commented to let you know my logic, just in case there's some misunderstanding in my concepts. Here's the code: </p>
  
  <pre><code> void modify(){
  
      int account_number;
      FILE *ptr;
      BankAccount account;
  
      ptr = fopen("account.txt", "r+");
      printf("Enter account number: ");
      fflush(stdin);
      scanf("%d", &amp;account_number);
  
      while (!feof(ptr)) // To search the whole "account.txt" file.
      {
          fread(&amp;account, sizeof(BankAccount), 1, ptr); //brings record into memory
          if (account.account_number == account_number){ // if record's account number is same as account number entered by user above
              printf("***Account found***\n\nAccount number: %d\nAccount name: %s\nAccount balance: %.2f\n", account.account_number, account.name, account.balance);
              printf("\nEnter new balance: ");
              fflush(stdin);
              scanf("%f", &amp;account.balance); // rewrites account's balance in memory
              fseek(ptr, -sizeof(BankAccount), SEEK_CUR); //pointer seeked to the beginning of the record to overwrite it with the one in memory
              fwrite(&amp;account,sizeof(BankAccount), 1, ptr); // record overwritten
              return;
  
          }
      }
  
  
      printf("Account not found\n");
      fflush(stdin);
      getch();
  }
  </code></pre>
  
  <p>Here's the whole .cpp file of my project in case you'd like to run it : <a href="https://dl.dropboxusercontent.com/u/63089169/Source.cpp" rel="nofollow">Source code</a>. I'd appreciate some guidance. Thanks in advance.</p>