bsc

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

root / semester_1 / letusc / luc089.c

luc089.c (2880B)


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