-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.cpp
More file actions
198 lines (177 loc) · 4.12 KB
/
Copy pathdictionary.cpp
File metadata and controls
198 lines (177 loc) · 4.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
#include <fstream>
#include<string.h>
using namespace std;
class dictionary
{
public:
char word[50];
private:
char meaning[100];
public:
void accept()
{
cout<<endl<<"enter word";
cin>>word;
cout<<endl<<"enter meaning";
cin>>meaning;
}
void display()
{
cout<<endl<<word<<"\t\t"<<meaning;
}
};
dictionary obj;
//Writing to a file
void addword()
{
obj.accept();
fstream wr;
wr.open("C:\\students\\mywords.txt",ios::app);
wr.write((char *)&obj,sizeof(obj));
cout<<endl<<"Object Written";
wr.close();
}
void readwords()
{
fstream rd;
rd.open("C:\\students\\mywords.txt",ios::in);
rd.seekg(0,ios::end);
//calculate the size of file
int n=rd.tellg();
//calculate no of objects
int nwords=n/sizeof(obj);
//bring file cursor to beginning
rd.seekg(0,ios::beg);
for(int i=1;i<=nwords;i++)
{
rd.read((char *)&obj,sizeof(obj));
obj.display();
}
}
void search()
{
char sr[50];
cout<<endl<<"Enter Word";
cin>>sr;
fstream rd;
rd.open("C:\\students\\mywords.txt");
rd.seekg(0,ios::end);
int n=rd.tellg();
int nwords=n/sizeof(obj);
rd.seekg(0,ios::beg);
for(int i=1;i<=nwords;i++)
{
rd.read((char *)&obj,sizeof(obj));
if(strcmp(sr,obj.word)==0)
{
obj.display();
}
}
}
void removeword()
{
fstream rd,wr;
rd.open("C:\\students\\mywords.txt",ios::in);
wr.open("C:\\students\\temp.txt",ios::out);
char s[50];
cout<<endl<<"Enter Word";
cin>>s;
rd.seekg(0,ios::end);
int n=rd.tellg();
int nwords=n/sizeof(obj);
rd.seekg(0,ios::beg);
for(int i=1;i<=nwords;i++)
{
rd.read((char *)&obj,sizeof(obj));
if(strcmp(obj.word,s)==0)
{
cout<<endl<<"Word Deleted Successfully";
}
else
{
wr.write((char *)&obj,sizeof(obj));
}
}
rd.close();
wr.close();
remove("C:\\students\\mywords.txt");
rename("C:\\students\\temp.txt","C:\\students\\mywords.txt");
}
void updateword()
{
fstream rd,wr;
rd.open("C:\\students\\mywords.txt",ios::in);
wr.open("C:\\students\\temp.txt",ios::out);
char s[50];
cout<<endl<<"Enter Word";
cin>>s;
rd.seekg(0,ios::end);
int n=rd.tellg();
int nwords=n/sizeof(obj);
rd.seekg(0,ios::beg);
for(int i=1;i<=nwords;i++)
{
rd.read((char *)&obj,sizeof(obj));
if(strcmp(obj.word,s)==0)
{
obj.accept();
}
wr.write((char *)&obj,sizeof(obj));
}
rd.close();
wr.close();
remove("C:\\students\\mywords.txt");
rename("C:\\students\\temp.txt","C:\\students\\mywords.txt");
}
main()
{
int e=0,choice;
cout<<endl<<"************************************************************************************************************************************************";
cout<<endl<<"\t\t\t\t\t\tWELCOME TO MY DICTIONARY \t\t\t\t\t\t";
cout<<endl<<"************************************************************************************************************************************************";
while(e!=1)
{
cout<<endl<<"Enter 1 to Add Word";
cout<<endl<<"Enter 2 to Read Word";
cout<<endl<<"Enter 3 to Search Word";
cout<<endl<<"Enter 4 to Delete Word";
cout<<endl<<"Enter 5 to Update Word";
cout<<endl<<"Enter 6 to Exit";
cout<<endl<<"Enter Your Choice";
cin>>choice;
switch(choice)
{
case 1:
{
addword();
break;
}
case 2:
{
readwords();
break;
}
case 3:
{
search();
break;
}
case 4:
{
removeword();
break;
}
case 5:
{
updateword();
break;
}
case 6:
{
e=1;
break;
}
}
}
}