Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is the relevant part of the code currently on display:</p> <pre><code> 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); </code></pre> <p>I will assume that error checking on the function return values is 'not needed'; it should be there, but it will complicate things slightly. However, I wouldn't trust myself to write a program without the error checking - too many things can go wrong for my tastes (and bitter experience).</p> <ul> <li>You read a record at offset 0 into the file. You then update the balance, seek to a position based on the client account number relative to the current position, and then write the data back.</li> </ul> <p>At another time, we can discuss the wisdom of the binary data format you are using; it is sufficient for the time being.</p> <p>Here's code that 'works':</p> <pre><code>#include &lt;stdio.h&gt; struct clientData { int acctNum; char lastName[15]; char firstName[10]; double balance; }; static void print_client(struct clientData *data) { printf("%04d %-15s %-15s %6.2f\n", data-&gt;acctNum, data-&gt;lastName, data-&gt;firstName, data-&gt;balance); } int main(void) { FILE *cfPtr; struct clientData client = {0, "Me", "Mine", 50.0}; double serviceCharge = 5.0; /* Initialize */ cfPtr = fopen("credit.dat", "wb"); fwrite(&amp;client, sizeof(struct clientData), 1, cfPtr); fclose(cfPtr); /* Update */ cfPtr = fopen("credit.dat", "rb+"); fread(&amp;client, sizeof(struct clientData), 1, cfPtr); print_client(&amp;client); client.balance -= serviceCharge; fseek(cfPtr, 0, SEEK_SET); fwrite(&amp;client, sizeof(struct clientData), 1, cfPtr); fclose(cfPtr); /* Validate */ cfPtr = fopen("credit.dat", "rb"); fread(&amp;client, sizeof(struct clientData), 1, cfPtr); fclose(cfPtr); print_client(&amp;client); return 0; } </code></pre> <hr> <p>This works too...it doesn't skip 0 client IDs because it doesn't generate any. But you can deal with that.</p> <pre><code>#include &lt;stdio.h&gt; struct clientData { int acctNum; char lastName[15]; char firstName[10]; double balance; }; static void print_client(const struct clientData *data) { printf("%04d %-15s %-15s %6.2f\n", data-&gt;acctNum, data-&gt;lastName, data-&gt;firstName, data-&gt;balance); } /* Initialize file */ static void init_file(const char *filename) { FILE *cfPtr = fopen(filename, "wb"); int i; for (i = 1; i &lt;= 30; i++) { struct clientData client = { i, "", "", i * 50.0}; sprintf(client.lastName, "Me (%d)", i); sprintf(client.firstName, "Mine (%d)", i); fwrite(&amp;client, sizeof(struct clientData), 1, cfPtr); } fclose(cfPtr); } static void print_file(const char *filename) { struct clientData client; FILE *cfPtr = fopen(filename, "rb"); while (fread(&amp;client, sizeof(struct clientData), 1, cfPtr) == 1) print_client(&amp;client); fclose(cfPtr); } int main(void) { FILE *cfPtr; double serviceCharge = 5.0; const char *filename = "credit.dat"; struct clientData client; init_file(filename); printf("Post-initialization: %s\n", filename); print_file(filename); /* Update */ cfPtr = fopen(filename, "rb+"); while (fread(&amp;client, sizeof(struct clientData), 1, cfPtr) == 1) { client.balance -= serviceCharge; fseek(cfPtr, -1 * (long)sizeof(struct clientData), SEEK_CUR); fwrite(&amp;client, sizeof(struct clientData), 1, cfPtr); fseek(cfPtr, 0, SEEK_CUR); } fclose(cfPtr); printf("Post-update: %s\n", filename); print_file(filename); return 0; } </code></pre> <p>See also the comments...</p>
 

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