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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
| #include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
struct Directory
{
string FIO;
int phone;
string mail;
}Two;
void Write(const char* filename, Directory* name,int size)
{
ofstream fout(filename, ios_base::app);
if (!fout)
{
cout << "Sorry, i cant do it\n";
return;
}
char sbuff[80];
cout << "It will write to txt after press enter => input (!) => pres another one button(enter)\n";
do
{
fout << "\n";
cout << "Enter your surname (Romashkan)" << "\n"; cout << ">>";
cin.ignore();
cin.getline(sbuff, 80);
fout << sbuff;
cout << "\n";
cout << "Enter your initials (A.R)" << "\n"; cout << ">>";
cin.getline(sbuff, 80);
fout << " ";
fout << sbuff;
cout << "\n";
cout << "Enter your Phone number+38(0--)_(---)_(--)_(--)" << "\n"; cout << ">>";
cin.getline(sbuff, 80);
fout << " ";
fout << sbuff;
cout << "\n";
cout << "Enter your Mail" << "\n"; cout << ">>";
cin.getline(sbuff, 80);
fout << " ";
fout << sbuff;
cout << ">>";
cin.getline(sbuff, 80);
cout << "\n";
if (*sbuff == '!') { break; }
} while (*sbuff != '!');
fout.close();
}
void in_put(const char* filename, Directory* name, int size)
{
ifstream fin(filename);
if (!fin)
{
cout << "Wrong file\n";
}
else
{
string buff;
string fname;
string ini;
int num_phone;
string mulo;
int i = 0;
while (!fin.eof())
{
getline(fin, buff);
stringstream ss(buff);
ss >> fname;
ss >> ini;
ss >> num_phone;
ss >> mulo;
fname = fname + " " + ini;
name[i].FIO = fname;
name[i].phone = num_phone;
name[i].mail = mulo;
i++;
}
}
fin.close();
}
void number_of_lines(const char* filename, int *pn)
{
ifstream fin(filename);
string buff;
while (!fin.eof())
{
if (getline(fin, buff))
{
(*pn)++;
}
}
// cout << "\nNumber of lines - " << *pn << "\n";
}
/*void SortByName(const char* filename, Directory* name, int size)
{
ifstream fin(filename);
string surname;
char line;
string line_file_text;
//int n = strlen(fin.eof());
int j = 0;
cout << "Enter surname for sort by him -";
while (!fin.eof())
{
getline(fin, surname);
j++;
for (int i = 0; i < size - j; i++)
{
if (surname[i] > surname[i + 1])
{
line = surname[i];
surname[i] = surname[i + 1];
surname[i + 1] = line;
}
}
}
fin.close();
}
*/
/*void Search(const char* filename,Directory* name) /////// Поиск по номеру строки
{
ifstream fin(filename);
string sline;
string line_file_text;
int nline=0;
int i = 0;
cout << "Enter the number of line you want to find - ";
cin >> nline;
cout << "\n";
do
{
getline(fin, sline);
i++;
line_file_text.insert(line_file_text.size(), sline);
line_file_text.insert(line_file_text.size(), "\n");
if (nline == i)
{
cout << "#\tFIO\t\tPhone num\tMail\n";
cout << line_file_text << "\n";
}
} while (!fin.eof());
fin.close();
}
*/
void Search(const char* filename, Directory* name)//Поиск по фамилии
{
ifstream fin(filename);
string sline;
string line_file_text;
string nline;
string tline;
cout << "Enter the subname of line you want to find - ";
cin.ignore();
getline(cin, nline);
cout << "\n";
// int i = 0;
do
{
// i++;
line_file_text.insert(line_file_text.size(), sline); /*дабавить строку*/
/*добавить перенос на слудующую строку*/
line_file_text.insert(line_file_text.size(), "\n");
if (nline.find(line_file_text))
{
cout << "#\tFIO\t\tPhone num\tMail\n";
cout << line_file_text.size() << "\n";// не могу передать целую строку
cout << "\n";
}
} while (getline(fin, sline));
fin.close();
}
void Edit_Profile(const char* filename, Directory* name, int size, int *pn)
{
ifstream fin(filename);
int i_number_line_edit = 0; //для хранения номера строки который нужно удалить
int i_number_line_now = 0; //счётчик строк
string line; //для хранения строки
string line_file_text; //для хранения текста файла
cout << "Enter the line number, which will you want to edit - ";
cin >> i_number_line_edit;
do
{
i_number_line_now++;
if (i_number_line_now == i_number_line_edit)
{
line_file_text.insert(line_file_text.size(), line); /*дабавить строку*/
/*добавить перенос на слудующую строку*/
line_file_text.insert(line_file_text.size(), "\n");
}
} while (getline(fin, line));
fin.close();
//теперь в line_file_text будет содержаться измененный текст файла, теперь можно перезаписать файл
}
void Destroy(const char* filename, Directory* name, int size, int *pn)// add string about person and destroy her
{
ifstream fin(filename);
int i_number_line_delete = 0; //для хранения номера строки который нужно удалить
int i_number_line_now = 0; //счётчик строк
string line; //для хранения строки
string line_file_text; //для хранения текста файла
cout << "Enter the line number, which will you want to destroy - ";
cin >> i_number_line_delete;
while (!fin.eof())
{
getline(fin, line);
i_number_line_now++;
if (!(i_number_line_now == i_number_line_delete))
{
line_file_text.insert(line_file_text.size(), line); /*дабавить строку*/
/*добавить перенос на слудующую строку*/
line_file_text.insert(line_file_text.size(), "\n");
/*if (line_file_text[i_number_line_now] == ' ')
{
line_file_text.erase(i_number_line_now, 1);
}*/
}
}
fin.close();
//теперь в line_file_text будет содержаться измененный текст файла, теперь можно перезаписать файл
ofstream file_out;
file_out.open("inform.txt", ios::trunc | ios::binary);
file_out.write(line_file_text.c_str(), line_file_text.size());
file_out.clear();
}
void Check(const char* filename,Directory* name)
{
ifstream fin(filename);
string tbluff;
if (!fin.is_open())
{
cout << "Sorry, but that file is not correct \n";
}
}
void Show(const char* filename,Directory* name, int size,int *pn)
{
cout << "#\tFIO\t\tPhone num\tMail\n";
for (int i = 0; i<*pn; i++)
{
cout << i + 1 << "\t" << name[i].FIO << "\t" << name[i].phone << "\t" << name[i].mail << "\n";
}
cout << "\n";
}
int main()
{
int const size = 40;
Directory name[size];
int n = 0;
Check("inform.txt",name);
in_put("inform.txt", name,size);
number_of_lines("inform.txt", &n);
bool job = true;
while (job)
{
cout << "1-Show directory \n2-ADD information about you on konsol\n3-Destroy info of directory\n4-Sort by name\n5-Edit Profile\n6-Number of lines\n7-Search\n0-Exit \n\n";
int numb;
cout << "Enter number to choice your number - ";
cin >> numb;
cout << "\n";
switch (numb)
{
case(1):
Show("inform.txt",name,size,&n);
break;
case(2):
Write("inform.txt", name,size);
in_put("inform.txt", name, size);
break;
case(3):
Destroy("inform.txt", name, size, &n);
in_put("inform.txt", name,size);
break;
case(4):
// SortByName("inform.txt", name, size);
in_put("inform.txt", name,size);
break;
case(5):
Destroy("inform.txt", name, size, &n);
Write("inform.txt", name, size);
in_put("inform.txt", name,size);
break;
case(6):
number_of_lines("inform.txt", &n);
cout << "Lines = " << n << "\n";
// in_put("inform.txt", name,size);
break;
case(7):
Search("info.txt", name);
break;
case(0):
job = false;
break;
default:
cout << "Sorry,but you entered a wrong number of choice \n";
cout << "\n";
break;
}
}
} |