Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> while ( !feof( readPtr ) ) { if ( client.acctNum != 0 ) { </code></pre> <p>At this point in the code, you haven't <em>read</em> anything. Nothing. :)</p> <p>You've opened the file, you've checked to see if you've reached the end of the file, but there's no code yet to read anything from the file, so your <code>if</code> condition is just checking against the initialized structure at the start.</p> <p>Looping over input typically works best if the input is read at the <em>top</em> of the loop. (There are exceptions, but this doesn't look like one of them.)</p> <p>Try this as a starting point for your loop:</p> <pre><code>while(fread(&amp;client, sizeof(struct clientData), 1, filePtr)) { if (client.acctNum) { client.balance -= charge; fseek(filePtr, - sizeof(struct clientData), SEEK_CUR); if (1 != fwrite(&amp;client, sizeof(struct clientData), 1, filePtr)) perror("Error writing to file"); } } </code></pre> <p>The error handling can probably be improved; maybe the whole edit should be aborted. (Mandatory record locking on the file could have <em>some</em> writes fail and some writes succeed, but that is pretty unlikely.)</p> <p><strong>Update</strong></p> <p>Your <code>fseek()</code> call is basing the new file position based on multiplying the account number by the size of the <code>struct clientData</code>. This works ONLY IF your records are sorted, no numbers are skipped, and it starts at <code>0</code> and works up. (Your example input file is not sorted; you even have a record with a <code>0</code> account number in the middle of 'valid' records.) So switch to <code>fseek(..., SEEK_CUR)</code> instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload