luc089.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Update 'CUSTOMER.DAT' balance using 'TRANSACTIONS.DAT' (Deposit/Withdrawal). Ensure balance doesn't fall below Rs. 100 on withdrawal.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#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);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102