/* * Author : Amit Dutta * Date : 08 Feb 2026 * Repo : https://github.com/notamitgamer/bsc * License : MIT License (See the LICENSE file for details) */ /* Update 'CUSTOMER.DAT' balance using 'TRANSACTIONS.DAT' (Deposit/Withdrawal). Ensure balance doesn't fall below Rs. 100 on withdrawal. */ /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(e) */ /* This file is auto-generated by a bot. */ /* This code is not compiled; it is for reference only. */ #include #include #include #include struct customer { int accno; char name[30]; float balance; }; struct trans { int accno; char trans_type; float amount; }; void create_files(); void update_customer(struct customer *c, int n, struct trans t); int main() { FILE *fc, *ft; struct customer cust[100]; struct trans t; int i, n_cust = 0; create_files(); // Generate dummy data // 1. Load all customers into memory fc = fopen("CUSTOMER.DAT", "rb"); if (fc == NULL) exit(1); while (fread(&cust[n_cust], sizeof(struct customer), 1, fc) == 1) { n_cust++; } fclose(fc); // 2. Process transactions sequentially ft = fopen("TRANSACTIONS.DAT", "rb"); if (ft == NULL) exit(2); printf("Processing Transactions...\n"); while (fread(&t, sizeof(struct trans), 1, ft) == 1) { update_customer(cust, n_cust, t); } fclose(ft); // 3. Write updated data back to CUSTOMER.DAT fc = fopen("CUSTOMER.DAT", "wb"); fwrite(cust, sizeof(struct customer), n_cust, fc); fclose(fc); printf("Update Complete. New Balances:\n"); for(i=0; i= 100) { c[i].balance -= t.amount; printf("Acc %d: Withdrew %.2f\n", t.accno, t.amount); } else { printf("Acc %d: Withdrawal denied (Min bal constraint)\n", t.accno); } } return; } } printf("Transaction Error: Acc %d not found\n", t.accno); } void create_files() { struct customer c[] = {{101, "A", 500}, {102, "B", 1000}, {103, "C", 200}}; struct trans t[] = {{101, 'D', 200}, {102, 'W', 500}, {103, 'W', 150}}; // 103 fail FILE *f1 = fopen("CUSTOMER.DAT", "wb"); FILE *f2 = fopen("TRANSACTIONS.DAT", "wb"); fwrite(c, sizeof(struct customer), 3, f1); fwrite(t, sizeof(struct trans), 3, f2); fclose(f1); fclose(f2); }