title: update random access file in c
body: <p>I want to do the following in C.</p>
  
  <p>I have a random access data file containing some records.</p>
  
  <p>The records are in the following format:</p>
  
  <pre><code>Acct#  First Name     Last Name    Balance
  0     ""             ""             0.0
  0     ""             ""             0.0
  0     ""             ""             0.0
  05    Joe             Costanza      0.50
   0     ""             ""             0.0
   0     ""             ""             0.0
   0     ""             ""             0.0
  19    Jason           Bourne        58.00
  0     ""              ""            0.0
  0     ""             ""             0.0
  42    Andy            Der          -15.12
  0     ""             ""             0.0
  0     ""             ""             0.0
  </code></pre>
  
  <p>I want to subtract an amount from balance from all the records that have a non zero account number and write the new updated balance to the file for those records.</p>
  
  <p>This is what I have tried so far to accomplish the above.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  struct clientData 
  { 
  int acctNum;          
  char lastName[15];  
  char firstName[10]; 
  double balance;       
  };
  
  void textFile(FILE *readPtr);
  
  int main(void)
  { 
       FILE *cfPtr;
  
       struct clientData client = {0, "", "", 0.0};
  
       double serviceCharge = 5.0;
  
       cfPtr = fopen("credit.dat", "rb+");
  
       fread(&amp;client, sizeof(struct clientData), 1, cfPtr);
  
       client.balance -= serviceCharge;
  
       fseek(cfPtr,(client.acctNum - 1) * sizeof(struct clientData)
       , SEEK_CUR);
  
       fwrite(&amp;client, sizeof(struct clientData), 1, cfPtr);
  
       fclose(cfPtr);
  
       return 0;
  }
  </code></pre>
  
  <p>No matter what I try, I just can't write a single updated record back to the file. I have even tried a single record without any while loop or if statement and it still does not work. Can anybody tell me what I am missing.</p>
  
  <p>loop:</p>
  
  <pre><code>fread( &amp;client, sizeof( struct clientData ), 1, cfPtr);
  
  while (!feof(cfPtr))
  { 
  if ( client.acctNum != 0 )
  {
      client.balance -= serviceCharge;
      fseek(cfPtr, (-1 * sizeof(struct clientData)), SEEK_CUR); 
  
      fwrite( &amp;client, sizeof( struct clientData ), 1, cfPtr);
  }
  
  fread( &amp;client, sizeof( struct clientData ), 1, cfPtr);
  }
  </code></pre>