bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc089.c (3071B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 08 Feb 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Update 'CUSTOMER.DAT' balance using 'TRANSACTIONS.DAT' (Deposit/Withdrawal). Ensure balance doesn't fall below Rs. 100 on withdrawal.
      9 */
     10 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(e) */
     11 
     12 /* This file is auto-generated by a bot. */
     13 /* This code is not compiled; it is for reference only. */
     14 
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <ctype.h>
     20 
     21 struct customer {
     22     int accno;
     23     char name[30];
     24     float balance;
     25 };
     26 
     27 struct trans {
     28     int accno;
     29     char trans_type;
     30     float amount;
     31 };
     32 
     33 void create_files();
     34 void update_customer(struct customer *c, int n, struct trans t);
     35 
     36 int main()
     37 {
     38     FILE *fc, *ft;
     39     struct customer cust[100];
     40     struct trans t;
     41     int i, n_cust = 0;
     42 
     43     create_files(); // Generate dummy data
     44 
     45     // 1. Load all customers into memory
     46     fc = fopen("CUSTOMER.DAT", "rb");
     47     if (fc == NULL) exit(1);
     48 
     49     while (fread(&cust[n_cust], sizeof(struct customer), 1, fc) == 1)
     50     {
     51         n_cust++;
     52     }
     53     fclose(fc);
     54 
     55     // 2. Process transactions sequentially
     56     ft = fopen("TRANSACTIONS.DAT", "rb");
     57     if (ft == NULL) exit(2);
     58 
     59     printf("Processing Transactions...\n");
     60     while (fread(&t, sizeof(struct trans), 1, ft) == 1)
     61     {
     62         update_customer(cust, n_cust, t);
     63     }
     64     fclose(ft);
     65 
     66     // 3. Write updated data back to CUSTOMER.DAT
     67     fc = fopen("CUSTOMER.DAT", "wb");
     68     fwrite(cust, sizeof(struct customer), n_cust, fc);
     69     fclose(fc);
     70 
     71     printf("Update Complete. New Balances:\n");
     72     for(i=0; i<n_cust; i++)
     73         printf("%d %s: %.2f\n", cust[i].accno, cust[i].name, cust[i].balance);
     74 
     75     return 0;
     76 }
     77 
     78 void update_customer(struct customer *c, int n, struct trans t)
     79 {
     80     int i;
     81     for (i = 0; i < n; i++)
     82     {
     83         if (c[i].accno == t.accno)
     84         {
     85             if (t.trans_type == 'D')
     86             {
     87                 c[i].balance += t.amount;
     88                 printf("Acc %d: Deposited %.2f\n", t.accno, t.amount);
     89             }
     90             else if (t.trans_type == 'W')
     91             {
     92                 if ((c[i].balance - t.amount) >= 100)
     93                 {
     94                     c[i].balance -= t.amount;
     95                     printf("Acc %d: Withdrew %.2f\n", t.accno, t.amount);
     96                 }
     97                 else
     98                 {
     99                     printf("Acc %d: Withdrawal denied (Min bal constraint)\n", t.accno);
    100                 }
    101             }
    102             return;
    103         }
    104     }
    105     printf("Transaction Error: Acc %d not found\n", t.accno);
    106 }
    107 
    108 void create_files()
    109 {
    110     struct customer c[] = {{101, "A", 500}, {102, "B", 1000}, {103, "C", 200}};
    111     struct trans t[] = {{101, 'D', 200}, {102, 'W', 500}, {103, 'W', 150}}; // 103 fail
    112     FILE *f1 = fopen("CUSTOMER.DAT", "wb");
    113     FILE *f2 = fopen("TRANSACTIONS.DAT", "wb");
    114     fwrite(c, sizeof(struct customer), 3, f1);
    115     fwrite(t, sizeof(struct trans), 3, f2);
    116     fclose(f1); fclose(f2);
    117 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror