-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_try_work.cpp
More file actions
348 lines (261 loc) · 7.19 KB
/
Copy pathfiles_try_work.cpp
File metadata and controls
348 lines (261 loc) · 7.19 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
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
/* Работа с файлами. Создание, запись, чтение */
// fstream - чтение/сохранение данных
// ifstream - чтение
// ofsteaam - запись
/* Создание, запись
ofstream fout;
// fout.open(path);
fout.open(path,ofstream::app); // добавление данных без затирания
if (!fout.is_open()){
cout << "Ошибка открытия файла!" << endl;
}else{
for (int i =0; i < 5; i++) {
cout << "Введите число" << endl;
int a;
cin >> a;
fout << a;
fout << "\n";
}
}
fout.close();*/
/* Чтение
ifstream fin;
fin.open(path);
if (!fin.is_open()) {
cout << "Ошибка открытия файла!" << endl;
}
else {
cout << "Файл открыт!" << endl;
// По байтам
//char ch;
//while (fin.get(ch)){
// cout << ch;
//}
// Целиком строкой
string str;
while (!fin.eof()) {
str = "";
getline(fin, str);
//fin >> str;
cout << str << endl;
}
}
fin.close();*/
/*class Point {
public:
Point() { x = y = z = 0; }
Point(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
void Print() { cout << "X: " << x << "\tY: " << y << "\tZ: " << z << endl; }
int x;
int y;
int z;
};
int main() {
setlocale(LC_ALL, "ru");
string path = "my_file.txt";
// Сохранение класса в файл
Point point(255, 120, 456);
//point.Print();
ofstream fout;
fout.open(path, ofstream::app); // добавление данных без затирания
if (!fout.is_open()) {
cout << "Ошибка открытия файла!" << endl;
}
else {
cout << "Файл открыт!" << endl;
fout.write((char*)&point, sizeof(Point));
}
fout.close();
// Чтение класса из файла
ifstream fin;
fin.open(path);
if (!fin.is_open()) {
cout << "Ошибка открытия файла!" << endl;
}
else {
cout << "Файл открыт!" << endl;
Point pnt;
while (fin.read((char*)&pnt, sizeof(Point))) {
pnt.Print();
}
}
fin.close();
return 0;
}
*/
/* fstream - чтение/сохранение данных
int main() {
setlocale(LC_ALL, "ru");
string path = "my_file.txt";
fstream fs;//флаги чтения записи добавления
fs.open(path, fstream::in | fstream::out | fstream::app);
if (!fs.is_open()){
cout << "Ошибка открытия файла!" << endl;
}else{
string msg;
int value;
cout << "Файл открыт!" << endl;
cout << "Нажмите 1 для записи сообщения в файл:" << endl;
cout << "Нажмите 2 для чтения всех сообщений из файла:" << endl;
cin >> value;
if (value == 1){
cout << "Введите ваше сообщение:" << endl;
SetConsoleCP(1251);
cin >> msg;
fs << msg << "\n";
SetConsoleCP(866);
}
if (value == 2) {
cout << "Чтение данных из файла:" << endl;
while (!fs.eof()) {
msg = "";
fs >> msg;
cout << msg << endl;
}
}
}
fs.close();
return 0;
}
*/
/* перегрузка операторов потокового ввода вывода
class Point {
public:
Point() { x = y = z = 0; }
Point(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
private:
int x;
int y;
int z;
friend ostream& operator << (ostream& os, const Point& point);
friend istream& operator >> (istream& is, Point& point);
};
ostream& operator << (ostream& os, const Point& point) {
os << point.x << " " << point.y << " " << point.z;
return os;
}
istream& operator >> (istream& is, Point& point) {
is >> point.x >> point.y >> point.z;
return is;
}
int main() {
setlocale(LC_ALL, "ru");
string path = "my_file.txt";
Point p(212, 7, 44);
//cout << p;
fstream fs;
fs.open(path, fstream::in | fstream::out | fstream::app);
if (!fs.is_open()) {
cout << "Ошибка открытия файла!" << endl;
}
else {
cout << "Файл открыт!" << endl;
//fs << p << "\n";
while (true) {
Point point;
fs >> point;
if (fs.eof()) {
break;
}
cout << point << endl;
}
}
fs.close();
return 0;
}
*/
/* Обработка исключений try, catch
int main() {
setlocale(LC_ALL, "ru");
string path = "my_file.tx";
ifstream fin;
fin.exceptions(ifstream::badbit | ifstream::failbit);
try{
fin.open(path);
cout << "Файл открыт" << endl;
}
catch (const ifstream::failure & ex){
cout << ex.what() << endl;
cout << ex.code() << endl;
cout << "Ошибка открытия файла!" << endl;
}
return 0;
}*/
/* throw
void Foo(int value) {
if (value<0){ throw exception("число меньше 0!!!"); }
cout << "Переменная = " << value << endl;
}
int main() {
setlocale(LC_ALL, "ru");
try{
Foo(-55);
}
catch (const exception &ex){
cout << "Мы поймали " << ex.what() << endl;
}
return 0;
}
*/
/* Несколько блоков catch
void Foo(int value) {
if (value<0){ throw "число меньше 0 !!!"; }
if (value == 0) { throw exception("число равно 0 !!!"); }
if (value == 1) { throw 1; }
cout << "Переменная = " << value << endl;
}
int main() {
setlocale(LC_ALL, "ru");
try{
Foo(1);
}
catch (const exception &ex){
cout << "Блок 1 Мы поймали " << ex.what() << endl;
}
catch(const char *ex){
cout << "Блок 2 Мы поймали " << ex << endl;
}
catch (...) {
cout << "Что то пошло не так!" << endl;
}
return 0;
}
*/
/* Создание собственного класса исключений
class MyException : public exception {
public:
MyException(const char *msg, int dataState):exception(msg) {
this->dataState = dataState;
}
int GetDataState() { return dataState; }
private:
int dataState;
};
void Foo(int value) {
if (value < 0) { throw exception("число меньше 0 !!!"); }
if (value == 1) { throw MyException("число равно 1 !!!",value); }
cout << "Переменная = " << value << endl;
}
int main() {
setlocale(LC_ALL, "ru");
try{
Foo(-1);
}
catch (MyException &ex){
cout << "Блок 1 Мы поймали " << ex.what() << endl;
cout << "Состояние данных " << ex.GetDataState() << endl;
}
catch (exception& ex) {
cout << "Блок Мы поймали " << ex.what() << endl;
}
return 0;
}
*/