-
Notifications
You must be signed in to change notification settings - Fork 0
/
RECORD.C
68 lines (65 loc) · 1.52 KB
/
RECORD.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
64
65
66
67
68
// dynamic file access
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *f;
char data[20], val[20];
int ch, found = 0;
f = fopen("data.dat", "a+b");
clrscr();
do
{
clrscr();
printf("\n1. Insert a record");
printf("\n2. Update a record");
printf("\n3. Delete a record");
printf("\n4. See all records");
printf("\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
scanf("%c");
switch(ch)
{
case 1: printf("Enter a name: ");
scanf("%[^\n]s", data);
fwrite(data, sizeof(data), 1, f);
printf("Record added.\n");
break;
case 2: found = 0;
printf("Enter a record to update: ");
scanf("%[^\n]s", data);
rewind(f);
while(fread(val, sizeof(val), 1, f))
{
if(strcmp(val, data) == 0)
{
found = 1;
scanf("%c");
printf("Enter new record: ");
scanf("%[^\n]s", data);
fseek(f, -sizeof(val), 1);
fwrite(data,sizeof(data), 1, f);
printf("Record updated.\n");
break;
}
}
if(found == 0)
printf("Record not present in the file.\n");
break;
case 3: printf("Record deletion not implemented yet.\n");
break;
case 4: printf("\nAll the records in file are: \n");
rewind(f);
while(fread(data, sizeof(data), 1, f))
{
printf("%s\n", data);
}
case 5: break;
default: printf("Invalid choice. Enter again...\n");
}
getch();
}while(ch != 5);
fclose(f);
}