luc089.c¶
Problem Statement
Update 'CUSTOMER.DAT' balance using 'TRANSACTIONS.DAT' (Deposit/Withdrawal). Ensure balance doesn't fall below Rs. 100 on withdrawal.
Metadata¶
| Property | Detail |
|---|---|
| Author | Amit Dutta amitdutta4255@gmail.com |
| Date | 08 Feb 2026 |
| License | MIT License (See the LICENSE file for details) |
Actions¶
💡 You can print or save this file by opening Raw and using your browser.
Source Code¶
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
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<n_cust; i++)
printf("%d %s: %.2f\n", cust[i].accno, cust[i].name, cust[i].balance);
return 0;
}
void update_customer(struct customer *c, int n, struct trans t)
{
int i;
for (i = 0; i < n; i++)
{
if (c[i].accno == t.accno)
{
if (t.trans_type == 'D')
{
c[i].balance += t.amount;
printf("Acc %d: Deposited %.2f\n", t.accno, t.amount);
}
else if (t.trans_type == 'W')
{
if ((c[i].balance - t.amount) >= 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);
}