-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandling22.c
131 lines (125 loc) · 2.99 KB
/
FileHandling22.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
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
/*
* C Program to Update Details of Employee using Files
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{
int empid;
char *name;
};
int count = 0;
void add_rec(char *a);
void display(char *a);
void update_rec(char *a);
void main(int argc, char *argv[])
{
int choice;
while (1)
{
printf("MENU:\n");
printf("1.Add a record\n");
printf("2.Display the file\n");
printf("3.Update the record\n");
printf("Enter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
add_rec(argv[1]);
break;
case 2:
display(argv[1]);
break;
case 3:
update_rec(argv[1]);
break;
case 4:
exit(0);
default:
printf("Wrong choice!!!\nEnter the correct choice\n");
}
}
}
void add_rec(char *a)
{
FILE *fp;
fp = fopen(a, "a+");
struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
temp->name = (char *)malloc(50*sizeof(char));
if (fp == NULL)
printf("Error!!!");
else
{
printf("Enter the employee id\n");
scanf("%d", &temp->empid);
fwrite(&temp->empid, sizeof(int), 1, fp);
printf("enter the employee name\n");
scanf(" %[^\n]s", temp->name);
fwrite(temp->name, 50, 1, fp);
count++;
}
fclose(fp);
free(temp);
free(temp->name);
}
void display(char *a)
{
FILE *fp;
char ch;
int rec = count;
fp = fopen(a, "r");
struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
temp->name = (char *)malloc(50*sizeof(char));
if (fp == NULL)
printf("Error!!");
else
{
while (rec)
{
fread(&temp->empid, sizeof(int), 1, fp);
printf("%d", temp->empid);
fread(temp->name, 50, 1, fp);
printf(" %s\n", temp->name);
rec--;
}
}
fclose(fp);
free(temp);
free(temp->name);
}
void update_rec(char *a)
{
FILE *fp;
char ch, name[5];
int rec, id, c;
fp = fopen(a, "r+");
struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
temp->name = (char *)malloc(50*sizeof(char));
printf("Enter the employee id to update:\n");
scanf("%d", &id);
fseek(fp, 0, 0);
rec = count;
while (rec)
{
fread(&temp->empid, sizeof(int), 1, fp);
printf("%d", temp->empid);
if (id == temp->empid)
{
printf("Enter the employee name to be updated");
scanf(" %[^\n]s", name);
c = fwrite(name, 50, 1, fp);
break;
}
fread(temp->name, 50, 1, fp);
rec--;
}
if (c == 1)
printf("Record updated\n");
else
printf("Update not successful\n");
fclose(fp);
free(temp);
free(temp->name);
}