-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.c
More file actions
174 lines (168 loc) · 5.12 KB
/
delete.c
File metadata and controls
174 lines (168 loc) · 5.12 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct data
{
char Product_name[100];
char Company_name[100];
char Price[20];
char Warranty[20];
char Quantity[20];
char Model[40];
};
void utl(char s[])/*Upper case to lower case conversion*/
{
int c=0;
while(s[c])
{
if(s[c]>='A' && s[c]<='Z')
{
s[c]=s[c]+32;
}
c++;
}
}
void delete()
{
FILE*fp;
FILE*ft;
int a;
int i=0;
int j;
struct data store;
struct data temp;
fp=fopen("automobile_database.txt","r");
if(fp==NULL)
{
printf("File not accessed");
return;
}
do
{
fscanf(fp,"%s %s %s %s %s %s",store.Product_name,store.Company_name,store.Price,store.Warranty,store.Quantity,store.Model);
i++;
}
while(!feof(fp));
printf("1.To delete a product with specified company name.\n2.To delete products with specified warranty\n0.To quit\n");
printf("\nEnter a number to perform the operation : ");
scanf("%d",&a);
while(1)
{
if(a!=1 && a!=0 && a!=2)
{
printf("Enter a valid option:");
scanf("%d",&a);
}
else
{
break;
}
}
fseek(fp,0,SEEK_SET);
if(a==1)
{
char input1[20];
char input2[20];
int k=0;
while (1)
{
printf("\nEnter Product name : ");
scanf("%s",input1);
utl(input1);
printf("Enter Company name : ");
scanf("%s",input2);
utl(input2);
k=0;
fseek(fp,0,SEEK_SET);
do
{
k++;
if(k>i)
{
break;
}
fscanf(fp,"%s %s %s %s %s %s",store.Product_name,store.Company_name,store.Price,store.Warranty,store.Quantity,store.Model);
strcpy(temp.Product_name,store.Product_name);
utl(store.Product_name);
utl(temp.Product_name);
strcpy(temp.Company_name,store.Company_name);
utl(temp.Company_name);
utl(store.Company_name);
}
while(strcmp(input1,temp.Product_name)!=0 || strcmp(input2,temp.Company_name)!=0 );
if(strcmp(input1,store.Product_name)==0 && strcmp(input2,store.Company_name)==0)
{
printf("\nThe specified item is deleted successfully.\n");
break;
}
if(k>i)
{
printf("\nUnable to find the company or the product you entered\n");
printf("Please enter a valid company and product name\n");
}
}
fseek(fp,0,SEEK_SET);
ft=fopen("temp.txt","a");
for(j=0;(j<i-1) && (!feof(fp));++j)
{
fscanf(fp,"%s %s %s %s %s %s",store.Product_name,store.Company_name,store.Price,store.Warranty,store.Quantity,store.Model);
strcpy(temp.Company_name,store.Company_name);
utl(temp.Company_name);
if(strcmp(input2,temp.Company_name)!=0)
{
fprintf(ft,"%s %s %s %s %s %s\n",store.Product_name,store.Company_name,store.Price,store.Warranty,store.Quantity,store.Model);
}
}
}
fseek(fp,0,SEEK_SET);
if(a==2)
{
char input3[20];
int k=0;
while (1)
{
printf("Enter Warranty(in years) : ");
scanf("%s",input3);
k=0;
fseek(fp,0,SEEK_SET);
do
{
k++;
if(k>i)
{
break;
}
fscanf(fp,"%s %s %s %s %s %s",store.Product_name,store.Company_name,store.Price,store.Warranty,store.Quantity,store.Model);
}
while(strcmp(input3,store.Warranty)!=0);
if(strcmp(input3,store.Warranty)==0)
{
printf("\nThe products with specified warranty have been successfully deleted.\n");
break;
}
if(k>i)
{
printf("No product found with the given warranty\nEnter a");
}
}
fseek(fp,0,SEEK_SET);
ft=fopen("temp.txt","a");
for(j=0;(j<i-1) && (!feof(fp));++j)
{
fscanf(fp,"%s %s %s %s %s %s",store.Product_name,store.Company_name,store.Price,store.Warranty,store.Quantity,store.Model);
//if((strcmp(input1,store.Product_name)!=0) && (strcmp(input2,store.Company_name)!=0))
if(strcmp(input3,store.Warranty)!=0)
{
fprintf(ft,"%s %s %s %s %s %s\n",store.Product_name,store.Company_name,store.Price,store.Warranty,store.Quantity,store.Model);
}
}
}
if(a==0)
{
return;
}
fclose(fp);
fclose(ft);
remove("automobile_database.txt");
rename("temp.txt","automobile_database.txt");
}