Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As promised, here is a quick exmaple of a father-son update. I have not bothered checking for file opening errors as you already have them in your code, and a real application would need to check more than just EOF for fscanf in order to detect invalid file formatting.</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #define bool int #define false 0 #define true !false #define MAX_ACCOUNT_NUMBER 99999 typedef struct { int accountNumber; char accountName[30]; float accountTotal; } ClientRecordType; typedef struct { int accountNumber; char transactionDate[30]; float transactionAmount; } TransactionRecordType; FILE *oldClientFile; FILE *newClientFile; FILE *transactionFile; void getNextClient(FILE *p_clientFile, ClientRecordType *p_clientRecord) { if (EOF == fscanf(p_clientFile, "%d%s%f", &amp;p_clientRecord-&gt;accountNumber, &amp;p_clientRecord-&gt;accountName, &amp;p_clientRecord-&gt;accountTotal)) p_clientRecord-&gt;accountNumber = MAX_ACCOUNT_NUMBER; } void getNextTransaction(FILE *p_transactionFile, TransactionRecordType *p_transactionRecord) { if (EOF == fscanf(p_transactionFile, "%d%s%f", &amp;p_transactionRecord-&gt;accountNumber, &amp;p_transactionRecord-&gt;transactionDate, &amp;p_transactionRecord-&gt;transactionAmount)) p_transactionRecord-&gt;accountNumber = MAX_ACCOUNT_NUMBER; } void writeUpdatedClientRecord(FILE *p_newClientFile, ClientRecordType *p_clientRecord) { fprintf(p_newClientFile, "%d %s %.2f ", p_clientRecord-&gt;accountNumber, p_clientRecord-&gt;accountName, p_clientRecord-&gt;accountTotal); } bool performTransactionUpdate(FILE *p_oldClientFile, FILE *p_newClientFile, FILE *p_transactionFile) { ClientRecordType clientRecord; TransactionRecordType transactionRecord; getNextClient(p_oldClientFile, &amp;clientRecord); getNextTransaction(p_transactionFile, &amp;transactionRecord); while (MAX_ACCOUNT_NUMBER != clientRecord.accountNumber) { if (clientRecord.accountNumber == transactionRecord.accountNumber) { clientRecord.accountTotal += transactionRecord.transactionAmount; getNextTransaction(p_transactionFile, &amp;transactionRecord); } else if (clientRecord.accountNumber &lt; transactionRecord.accountNumber) { writeUpdatedClientRecord(p_newClientFile, &amp;clientRecord); getNextClient(p_oldClientFile, &amp;clientRecord); } else { return false; } } if (MAX_ACCOUNT_NUMBER != transactionRecord.accountNumber) return false; return true; } int main(int argc, char *argv[]) { oldClientFile = fopen("clients.dat", "r"); newClientFile = fopen("newclients.dat", "w"); transactionFile = fopen("transactions.dat", "r"); if (performTransactionUpdate(oldClientFile, newClientFile, transactionFile)) printf("\nUpdate completed without errors!\n"); else printf("\nUnsorted files or invalid transactions encountered\n"); close(newClientFile); close(oldClientFile); close(transactionFile); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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