Repository navigation
Expand file tree
/
Copy pathdiary_management_system.c
More file actions
301 lines (229 loc) · 6.43 KB
/
Copy pathdiary_management_system.c
File metadata and controls
301 lines (229 loc) · 6.43 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//diary management system
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//Global variable used for loop
int i;
//Structure to hold each record
struct record{
char date[15];
char time[10];
char location[25];
char note[500];
};
//Function Prototypes
//function to add new record
void addRecord();
//function to display record
void viewRecord();
//function to update record
void updateRecord();
//function to delete record
void deleteRecord();
//function to display the header
void display();
void main(){
int choice;
char ch;
do {
display();
//displaying the main menu options
printf("\n\n\t\t\t\t\t\t MAIN MENU\n");
printf("\n\t\t\t\t PRESS[1]:Add Record");
printf("\n\t\t\t\t PRESS[2]:View Record");
printf("\n\t\t\t\t PRESS[3]:Update Record");
printf("\n\t\t\t\t PRESS[4]:Delete Record");
printf("\n\t\t\t\t PRESS[5]:Exit\n\n\t\t\t\t");
for (i=0; i<52; i++){
printf("=");
}
printf("\n\n\t\t\t\t\t PRESS KEY OF YOUR CHOICE : ");
scanf("%d", &choice);
switch(choice){
case 1:
addRecord();
break;
case 2:
viewRecord();
break;
case 3:
updateRecord();
break;
case 4:
deleteRecord();
break;
case 5:
printf("\n\t\t\t\tEXITING........\n");
printf("\n\t\t\t\t\t <<THANK YOU FOR USING THE PROGRAM>>\n");
getch();
exit(0);
default:
printf("\n\t\t\t\t\t\t INVALID CHOICE\n");
printf("\n\t\t\t\t\t PRESS ANY KEY TO TRY AGAIN");
getch();
break;
}
fflush(stdin);
printf("\n\n\t\t\t\tDo you want to choose again(Y/N)? : ");
scanf("%c", &ch);
}while(ch=='y' || ch=='Y');
printf("\n\t\t\t\tEXITING........\n");
printf("\n\t\t\t\t\t <<THANK YOU FOR USING THE PROGRAM>>\n");
getch();
}
void addRecord(){
FILE *fp ;
struct record r;
char fileName[15];
display();
printf("\n\n\t\t\t\t\t\t WELCOME TO ADD RECORD");
printf("\n\n\t\t\t\tEnter file name : ");
scanf("%s", fileName);
//opening file in write mode
fp=fopen(fileName,"wb+");
//checking if the file is opened successfully
if(fp==NULL){
printf("\n\t\t\t\t\t\tSYSTEM ERROR...");
printf("\n\t\t\t\tPRESS ANY KEY TO EXIT");
getch();
return;
}
//clearing the input buffer
getchar();
//getting record details from the user
printf("\t\t\t\tEnter Date(dd/mm/yyy) : ");
fgets(r.date, sizeof(r.date), stdin);
printf("\t\t\t\tEnter Time(hh:mm(am/pm)) : ");
fgets(r.time, sizeof(r.time), stdin);
printf("\t\t\t\tEnter Location : ");
fgets(r.location, sizeof(r.location), stdin);
printf("\n\t\t\t\tEnter note : \n\n\t\t\t\t");
fgets(r.note, sizeof(r.note), stdin);
//writing record to the file
fwrite(&r, sizeof(r),1,fp);
printf("\n\t\t\t\t\t\t YOUR RECORD IS ADDED...\n");
printf("\n\t\t\t\t");
for (i=0; i<52; i++){
printf("=");
}
//closing file
fclose(fp);
}
void viewRecord(){
FILE *fpn;
struct record r2;
char fileName[15];
display();
printf("\n\n\t\t\t\t\t\t WELCOME TO VIEW RECORD");
printf("\n\n\t\t\t\tEnter file name that you want to view : ");
scanf("%s", fileName);
fpn=fopen( fileName,"rb");
//checking if the file is opened successfully
if(fpn==NULL){
printf("\n\t\t\t\t\t\tTHE RECORD DOES NOT EXIST..." ) ;
printf("\n\n\t\t\t\tPRESS ANY KEY TO EXIT");
getch();
return;
}
//displaying the contents of the file
printf("\n\t\t\t\tThe contents of the file %s are : \n", fileName);
while(fread(&r2,sizeof(r2),1,fpn) == 1 ){
printf("\n\t\t\t\t\t\t\t\tDate : %s", r2.date);
printf("\n\t\t\t\t\t\t\t\tTime : %s", r2.time);
printf("\n\t\t\t\t\t\t\t\tLocation : %s", r2.location);
printf("\n\t\t\t\tNote :\n\n\t\t\t\t%s\n", r2.note);
}
printf("\n\t\t\t\t");
for (i=0; i<52; i++){
printf("=");
}
//closing the file
fclose(fpn);
printf("\n\n\t\t\t\t\t PRESS ANY KEY TO EXIT...");
getch();
}
void updateRecord(){
FILE *fp;
FILE *ftemp;
struct record r, temp;
char fileName[15];
display();
printf("\n\n\t\t\t\t\t\t WELCOME TO UPDATE RECORD");
printf("\n\n\t\t\t\tEnter file name to update : ");
scanf("%s", fileName);
//opening file in read mode
fp = fopen(fileName,"rb");
//checking if the file is opened successfully
if(fp == NULL){
printf("\n\t\t\t\t\t\tFILE DOES NOT EXIST...");
printf("\n\n\t\t\t\tPRESS ANY KEY TO EXIT");
getch();
return;
}
//opening temporary file for writing
ftemp = fopen("tempfile.bin", "wb+");
//checking if the file is opened successfully
if(ftemp == NULL){
printf("\n\t\t\t\t\t\tSYSTEM ERROR...");
fclose(fp);
getch();
return;
}
//clearing input buffer
getchar();
//getting record details from the user
printf("\t\t\t\tEnter New Date(dd/mm/yyyy) : ");
fgets(r.date, sizeof(r.date), stdin);
printf("\t\t\t\tEnter New Time(hh:mm(am/pm)) : ");
fgets(r.time, sizeof(r.time), stdin);
printf("\t\t\t\tEnter New Location : ");
fgets(r.location, sizeof(r.location), stdin);
printf("\n\t\t\t\tEnter New note : \n\n\t\t\t\t");
fgets(r.note, sizeof(r.note), stdin);
fwrite(&r, sizeof(r), 1, ftemp);
printf("\n\t\t\t\t\t\t RECORD UPDATED...\n");
//closing original file
fclose(fp);
//closing temporary file
fclose(ftemp);
//removing original file
remove(fileName);
//renaming the temporary file with the name of the file we just deleted
rename("tempfile.bin", fileName);
printf("\n\t\t\t\t");
for (i=0; i<52; i++){
printf("=");
}
printf("\n\n\t\t\t\t\t PRESS ANY KEY TO EXIT...");
getch();
}
void deleteRecord(){
char fileName[15];
display();
printf("\n\n\t\t\t\t\t\t WELCOME TO DELETE RECORD");
printf("\n\n\t\t\t\tEnter file name that you want to delete : ");
scanf("%s", fileName);
//deleting the file
if (remove(fileName) == 0) {
printf("\n\n\t\t\t\t File %s has been deleted successfully...\n", fileName);
} else {
printf("\n\n\t\t\t\t Failed to delete the file %s...\n", fileName);
}
printf("\n\t\t\t\t");
for (i=0; i<52; i++){
printf("=");
}
printf("\n\n\t\t\t\t\t PRESS ANY KEY TO EXIT...");
getch();
}
void display(){
system("cls");
printf("\n\n\t\t\t\t");
for (i=0; i<52; i++){
printf("=");
}
printf("\n\t\t\t\t\t DIARY MANAGEMENT SYSTEM\n\t\t\t\t");
for (i=0; i<52; i++){
printf("=");
}
}