-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditAccount.c
63 lines (62 loc) · 1.89 KB
/
editAccount.c
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
#include "myheader.h"
#include <stdio.h>
#include<string.h>
void editAccount(){
FILE *fp, *ftemp;
fp = fopen("data.txt", "r");
ftemp = fopen("temp.txt", "w");
if (fp == NULL || ftemp == NULL) {
puts("Something went wrong");
return;
}
User user;
setColor(BLUE);
puts("Edit Account");
drawLine(20);
resetColor();
printf("Enter account no: ");
int ac_no;
scanf("%d", &ac_no);
int user_found = 0;
while (!feof(fp)) {
fscanf(fp, "%d:\n", &user.ac_no);
fgets(user.name, 100, fp);
user.name[strcspn(user.name, "\n")] = 0;
fscanf(fp, "%d %s %d\n", &user.age, user.ac_type, &user.balance);
if (ac_no == user.ac_no) {
user_found = 1;
printf("Edit %s's account\n", user.name);
fflush(stdin);
printf("Enter new name: ");
gets(user.name);
printf("Enter new age: ");
scanf("%d", &user.age);
setColor(BLUE);
puts("Account Type:");
setColor(GREEN);
puts("1. Savings\n2. Current");
resetColor();
int x;
scanf("%d", &x);
if (x == 1) strcpy(user.ac_type, "savings");
else if (x == 2) strcpy(user.ac_type, "current");
else {
showErrorMessage();
remove("temp.txt");
return;
}
}
fprintf(ftemp, "%d:\n%s\n%d %s %d\n", user.ac_no, user.name, user.age, user.ac_type, user.balance);
}
fclose(fp);
fclose(ftemp);
if (!user_found) {
puts("No user found");
remove("temp.txt");
return;
} else {
remove("data.txt");
rename("temp.txt", "data.txt");
puts("Account edited successfully");
}
}