Вылетает программа
02.06.2022, 04:10. Показов 1466. Ответов 21
Здравствуйте все!
Разбираюсь с сохранением данных в файл. Программа вылетает при записи новых данных на new, или на открытии потока перед записью в файл. Объясните пожалуйста, что я делаю не так. Писал по примеру и даже записывали и читало из файла. Спасибо всем огромное.
| 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
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
| #include <iostream>
#include <fstream>
#include <string.h>
#include <windows.h>
using namespace std;
class DIRECTORY {
char* CName; // Variable for Company Name
char* Owner; // -// - Company Owner
char* Phone; // -//- Company Phone Number
char* Address; // -//- Adress
char* Occupation; // -//- Occupation
public:
// Constructor
DIRECTORY();
// Constructor with parameters
DIRECTORY(char* c, char* o, char* p, char* a, char* oc);
// Destructor
~DIRECTORY();
// Function of data input
void Input();
// Data display function
void Display();
// Storage data function
void FileSave();
// Display from file function
static void DisplayFromFile();
};
// Constructor
DIRECTORY::DIRECTORY() {
CName = 0;
Owner = 0;
Phone = 0;
Address = 0;
Occupation = 0;
}
// Constructor with parameters
DIRECTORY::DIRECTORY(char* n, char* o, char* p, char* a, char* oc) {
CName = new char[strlen(n) + 1];
if (!CName) {
cout << "Memory allocation error for 'CName' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(CName, strlen(CName), n);
Owner = new char[strlen(o) + 1];
if (!Owner) {
cout << "Memory allocation error for 'Owner' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Owner, strlen(Owner), o);
Phone = new char[strlen(p) + 1];
if (!Phone) {
cout << "Memory allocation error for 'Phone' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Phone, strlen(Phone), p);
Address = new char[strlen(a) + 1];
if (!Address) {
cout << "Memory allocation error for 'Address' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Address, strlen(Address), a);
Occupation = new char[strlen(oc) + 1];
if (!Occupation) {
cout << "Memory allocation error for 'Occupation' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Occupation, strlen(Occupation), o);
}
// Destructor
DIRECTORY::~DIRECTORY() {
delete[] CName;
delete[] Owner;
delete[] Phone;
delete[] Address;
delete[] Occupation;
}
// Input data function
void DIRECTORY::Input() {
char temp[1024];
cout << "Please enter Company Name:\r\n";
cin >> temp;
if (CName) {
delete[]CName;
}
CName = new char[strlen(temp) + 1];
if (!CName) {
cout << "Memory allocation error for variable 'CName'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(CName, strlen(CName), temp);
cout << "Please enter Company Owner:\r\n";
cin >> temp;
if (Owner) {
delete[]Owner;
}
Owner = new char[strlen(temp) + 1];
if (!Owner) {
cout << "Memory allocation error for variable 'Owner'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Owner, strlen(Owner), temp);
cout << "Please enter Company Phone Number:\r\n";
cin >> temp;
if (Phone) {
delete[]Phone;
}
Phone = new char[strlen(temp) + 1];
if (!Phone) {
cout << "Memory allocation error for variable 'Phone'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Phone, strlen(Phone), temp);
cout << "Please enter Company Address:\r\n";
cin >> temp;
if (Address) {
delete[]Address;
}
Address = new char[strlen(temp) + 1];
if (!Address) {
cout << "Memory allocation error for variable 'Phone'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Address, strlen(Address), temp);
cout << "Please enter type of Company (occupation):\r\n";
cin >> temp;
if (Occupation) {
delete[]Occupation;
}
Occupation = new char[strlen(temp) + 1];
if (!Occupation) {
cout << "Memory allocation error for variable 'Phone'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Occupation, strlen(Occupation), temp);
}
// Display data function
void DIRECTORY::Display() {
cout << "Company: " << CName << "\r\n";
cout << "Owner: " << Owner << "\r\n";
cout << "Phone: " << Phone << "\r\n";
cout << "Address: " << Address << "\r\n";
cout << "Type of Company: " << Occupation << "\r\n";
}
// Save to file function
void DIRECTORY::FileSave() {
fstream f("Directory.txt", ios::out | ios::binary | ios::app);
if (!f) {
cout << "File can't be open for writing data.\r\n";
}
// Count the number of characters in 'CName'
int size = strlen(CName);
// Writing amount of characters in 'CName'
f.write((char*)&size, sizeof(int));
// Writing 'CName'
f.write((char*)CName, size * sizeof(char));
// Count characters in 'Owner'
size = strlen(Owner);
// Writing amount of characters in 'Owner'
f.write((char*)&size, sizeof(int));
// Writing 'Owner'
f.write((char*)Owner, size * sizeof(char));
// Count characters in 'Phone'
size = strlen(Phone);
// Writing amount of characters in 'Phone'
f.write((char*)&size, sizeof(int));
// Writing 'Phone'
f.write((char*)Phone, size * sizeof(char));
// Count characters in 'Address'
size = strlen(Address);
// Writing amount of characters in 'Address'
f.write((char*)&size, sizeof(int));
// Writing 'Address'
f.write((char*)Address, size * sizeof(char));
// Count characters amount in 'Occupation'
size = strlen(Occupation);
// Writing amount of charachters in 'Occupation'
f.write((char*)&size, sizeof(int));
// Writing 'Occupation'
f.write((char*)Occupation, size * sizeof(char));
f.close();
}
// Function to Read & Display data from file
void DIRECTORY::DisplayFromFile() {
fstream d("Directory.txt", ios::in | ios::binary);
if (!d) {
cout << "Can't open file for reading data.\r\n";
Sleep(1500);
exit(0);
}
char* n, * o, * p, * a, * occ;
int temp = 0;
while (!d.eof()) // While reading data to temp (amount of symbols in 'CName' in this case)
{
d.read((char*)&temp, sizeof(int));
cout << "Company name: ";
// Memory allocation for 'CName'
n = new char[temp + 1];
if (!n) {
cout << "Memory allocation error for 'Company Name' data!\r\n";
Sleep(1500);
exit(1);
}
// Reading 'CName'
d.read(n, temp * sizeof(char));
n[temp] = '\0';
// Displaying 'CName'
cout << n << "\r\n";
cout << "Owner: ";
// Reading amount of symbols in 'Owner'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Owner'
o = new char[temp + 1];
// Reading 'Owner'
d.read(o, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Owner' data.\r\n";
Sleep(1500);
exit(1);
}
// Displaying 'Owner'
cout << o << "\r\n";
cout << "Phone: ";
// Reading amount of symbols in 'Phone'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Phone'
p = new char[temp + 1];
// Reading 'Phone'
d.read(p, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Phone' data.\r\n";
Sleep(1500);
exit(1);
}
// Displaying 'Phone'
cout << p << "\r\n";
cout << "Address: ";
// Reading amount of symbols in 'Address'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Address'
a = new char[temp + 1];
// Reading 'Address'
d.read(a, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Address' data.\r\n";
Sleep(1500);
exit(1);
}
// Displaying 'Address'
cout << a << "\r\n";
cout << "Occupation: ";
// Reading amount of symbols in 'Occupation'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Occupation'
occ = new char[temp + 1];
// Reading 'Occupation'
d.read(occ, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Occupation' data.\r\n";
Sleep(1500);
exit(1);
}
// Displaying 'Occupation'
cout << a << "\r\n";
cout << "\r\n";
delete[] n;
delete[] o;
delete[] p;
delete[] a;
delete[] occ;
}
//d.close(); //?
}
int main(int argc, char* argv[])
{
DIRECTORY* f;
int choice = 0;
do {
cout << "Please enter '1' if You want add data into DIRECTORY\r\n" <<
" or '2' if You wand load data from file\r\n" <<
"any other symbol to 'Exit' the programm.\r\n";
cin >> choice;
switch (choice) {
case 1:
f = new DIRECTORY;
f->Input();
f->FileSave();
delete f;
break;
case 2:
DIRECTORY::DisplayFromFile();
break;
default:
cout << "Good bye.\r\n";
return 1;
}
} while (1);
Sleep(5000);
return 0;
} |
|
Добавлено через 3 часа 10 минут
Спасибо всем большое. Исправил некоторые мелкие ошибки. Пока что пытаюсь понять, почему через раз выдает ошибку:
Unhandled exception at 0x770BA619 (ntdll.dll) in CodeW.exe: 0xC0000374: Куча была повреждена (parameters: 0x770F68C8).
на строке:
fstream f("Directory.txt", ios::out | ios::binary | ios::app);
| 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
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
| #include <iostream>
#include <fstream>
#include <string.h>
#include <windows.h>
using namespace std;
class DIRECTORY {
char* CName; // Variable for Company Name
char* Owner; // -// - Company Owner
char* Phone; // -//- Company Phone Number
char* Address; // -//- Adress
char* Occupation; // -//- Occupation
public:
// Constructor
DIRECTORY();
// Constructor with parameters
DIRECTORY(char* c, char* o, char* p, char* a, char* oc);
// Destructor
~DIRECTORY();
// Function of data input
void Input();
// Data display function
void Display();
// Storage data function
void FileSave();
// Display from file function
static void DisplayFromFile();
};
// Constructor
DIRECTORY::DIRECTORY() {
CName = 0;
Owner = 0;
Phone = 0;
Address = 0;
Occupation = 0;
}
// Constructor with parameters
DIRECTORY::DIRECTORY(char* n, char* o, char* p, char* a, char* oc) {
CName = new char[strlen(n) + 1];
if (!CName) {
cout << "Memory allocation error for 'CName' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(CName, strlen(CName), n);
Owner = new char[strlen(o) + 1];
if (!Owner) {
cout << "Memory allocation error for 'Owner' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Owner, strlen(Owner), o);
Phone = new char[strlen(p) + 1];
if (!Phone) {
cout << "Memory allocation error for 'Phone' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Phone, strlen(Phone), p);
Address = new char[strlen(a) + 1];
if (!Address) {
cout << "Memory allocation error for 'Address' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Address, strlen(Address), a);
Occupation = new char[strlen(oc) + 1];
if (!Occupation) {
cout << "Memory allocation error for 'Occupation' in constructor\r\n";
Sleep(1500);
exit(1);
}
strcpy_s(Occupation, strlen(Occupation), oc);
}
// Destructor
DIRECTORY::~DIRECTORY() {
delete[] CName;
delete[] Owner;
delete[] Phone;
delete[] Address;
delete[] Occupation;
}
// Input data function
void DIRECTORY::Input() {
char temp[1024];
cout << "Please enter Company Name:\r\n";
cin >> temp;
if (CName) {
delete[]CName;
}
CName = new char[strlen(temp) + 1];
if (!CName) {
cout << "Memory allocation error for variable 'CName'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(CName, strlen(CName), temp);
cout << "Please enter Company Owner:\r\n";
cin >> temp;
if (Owner) {
delete[]Owner;
}
Owner = new char[strlen(temp) + 1];
if (!Owner) {
cout << "Memory allocation error for variable 'Owner'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Owner, strlen(Owner), temp);
cout << "Please enter Company Phone Number:\r\n";
cin >> temp;
if (Phone) {
delete[]Phone;
}
Phone = new char[strlen(temp) + 1];
if (!Phone) {
cout << "Memory allocation error for variable 'Phone'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Phone, strlen(Phone), temp);
cout << "Please enter Company Address:\r\n";
cin >> temp;
if (Address) {
delete[]Address;
}
Address = new char[strlen(temp) + 1];
if (!Address) {
cout << "Memory allocation error for variable 'Phone'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Address, strlen(Address), temp);
cout << "Please enter type of Company (occupation):\r\n";
cin >> temp;
if (Occupation) {
delete[]Occupation;
}
Occupation = new char[strlen(temp) + 1];
if (!Occupation) {
cout << "Memory allocation error for variable 'Phone'\r\n";
Sleep(1500);
exit(0);
}
strcpy_s(Occupation, strlen(Occupation), temp);
}
// Display data function
void DIRECTORY::Display() {
cout << "Company: " << CName << "\r\n";
cout << "Owner: " << Owner << "\r\n";
cout << "Phone: " << Phone << "\r\n";
cout << "Address: " << Address << "\r\n";
cout << "Type of Company: " << Occupation << "\r\n\r\n";
}
// Save to file function
void DIRECTORY::FileSave() {
fstream f("Directory.txt", ios::out | ios::binary | ios::app);
if (!f) {
cout << "File can't be open for writing data.\r\n";
}
// Count the number of characters in 'CName'
int size = strlen(CName);
// Writing amount of characters in 'CName'
f.write((char*)&size, sizeof(int));
// Writing 'CName'
f.write((char*)CName, size * sizeof(char));
// Count characters in 'Owner'
size = strlen(Owner);
// Writing amount of characters in 'Owner'
f.write((char*)&size, sizeof(int));
// Writing 'Owner'
f.write((char*)Owner, size * sizeof(char));
// Count characters in 'Phone'
size = strlen(Phone);
// Writing amount of characters in 'Phone'
f.write((char*)&size, sizeof(int));
// Writing 'Phone'
f.write((char*)Phone, size * sizeof(char));
// Count characters in 'Address'
size = strlen(Address);
// Writing amount of characters in 'Address'
f.write((char*)&size, sizeof(int));
// Writing 'Address'
f.write((char*)Address, size * sizeof(char));
// Count characters amount in 'Occupation'
size = strlen(Occupation);
// Writing amount of charachters in 'Occupation'
f.write((char*)&size, sizeof(int));
// Writing 'Occupation'
f.write((char*)Occupation, size * sizeof(char));
f.close();
}
// Function to Read & Display data from file
void DIRECTORY::DisplayFromFile() {
fstream d("Directory.txt", ios::in | ios::binary);
if (!d) {
cout << "Can't open file for reading data.\r\n";
Sleep(1500);
exit(0);
}
char* n, * o, * p, * a, * occ;
int temp = 0;
while (!d.eof()) // While reading data to temp (amount of symbols in 'CName' in this case)
{
d.read((char*)&temp, sizeof(int));
cout << "Company name: ";
// Memory allocation for 'CName'
n = new char[temp + 1];
if (!n) {
cout << "Memory allocation error for 'Company Name' data!\r\n";
Sleep(1500);
exit(1);
}
// Reading 'CName'
d.read(n, temp * sizeof(char));
n[temp] = '\0';
// Displaying 'CName'
cout << n << "\r\n";
cout << "Owner: ";
// Reading amount of symbols in 'Owner'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Owner'
o = new char[temp + 1];
// Reading 'Owner'
d.read(o, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Owner' data.\r\n";
Sleep(1500);
exit(1);
}
o[temp] = '\0';
// Displaying 'Owner'
cout << o << "\r\n";
cout << "Phone: ";
// Reading amount of symbols in 'Phone'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Phone'
p = new char[temp + 1];
// Reading 'Phone'
d.read(p, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Phone' data.\r\n";
Sleep(1500);
exit(1);
}
// Displaying 'Phone'
p[temp] = '\0';
cout << p << "\r\n";
cout << "Address: ";
// Reading amount of symbols in 'Address'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Address'
a = new char[temp + 1];
// Reading 'Address'
d.read(a, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Address' data.\r\n";
Sleep(1500);
exit(1);
}
a[temp] = '\0';
// Displaying 'Address'
cout << a << "\r\n";
cout << "Occupation: ";
// Reading amount of symbols in 'Occupation'
d.read((char*)&temp, sizeof(int));
// Memory allocation for 'Occupation'
occ = new char[temp + 1];
// Reading 'Occupation'
d.read(occ, temp * sizeof(char));
if (!d) { // Check if memory allocation successful
cout << "Error during memory allocation for 'Occupation' data.\r\n";
Sleep(1500);
exit(1);
}
occ[temp] = '\0';
// Displaying 'Occupation'
cout << occ << "\r\n";
cout << "\r\n";
delete[] n;
delete[] o;
delete[] p;
delete[] a;
delete[] occ;
}
d.close();
}
int main(int argc, char* argv[])
{
DIRECTORY dir(_strdup("Comp"), _strdup("Merrel"), _strdup("+dfdfd"), _strdup("Sibbet"), _strdup("dfdfd"));
dir.Display();
dir.FileSave();
dir.DisplayFromFile();
/*
DIRECTORY* f;
int choice = 0;
do {
cout << "Please enter '1' if You want add data into DIRECTORY\r\n" <<
" or '2' if You wand load data from file\r\n" <<
"any other symbol to 'Exit' the programm.\r\n";
cin >> choice;
switch (choice) {
case 1:
f = new DIRECTORY;
f->Input();
f->FileSave();
delete f;
break;
case 2:
DIRECTORY::DisplayFromFile();
break;
default:
cout << "Good bye.\r\n";
return 1;
}
} while (1);
*/
Sleep(5000);
return 0;
} |
|
1
|