#include <iostream> #include <string> #include <stdio.h> #include <locale.h> #include <iomanip> #include <windows.h> using namespace std; const int N = 10; struct tovar { string name; float kol; float price; string proiz; }tov[N]; void quickSortByPrice(tovar* tov, int left, int right) { int i = left, j = right; tovar pivot = tov[(left + right) / 2]; while (i <= j) { while (tov[i].price < pivot.price) i++; while (tov[j].price > pivot.price) j--; if (i <= j) { swap(tov[i], tov[j]); i++; j--; } } if (left < j) quickSortByPrice(tov, left, j); if (i < right) quickSortByPrice(tov, i, right); } int main() { FILE* file1; SetConsoleCP(1251); SetConsoleOutputCP(1251); int A; while (true) { cout << "Что вы хотите сделать??" << endl; cout << "1. Отсортировать по цене" << endl; cout << "2. Отсортировать по имени" << endl; cout << "3. Отсортировать по Колличеству" << endl; cout << "4. Выход" << endl; cout << "5. Дополнить таблицу" << endl; cout << "6. Вывести таблицу" << endl; cin >> A; switch (A) { case 1: { file1 = fopen("inf.txt", "rb"); int currentSize = 0; while (fread(&tov[currentSize], sizeof(tov[currentSize]), 1, file1) == 1) { currentSize++; } quickSortByPrice(tov, 0, currentSize - 1); cout << "|" << setw(44) << setfill('-') << "Таблицы" << setw(20) << "|" << endl; cout << "|" << setw(18) << setfill(' ') << "Наименование" << "|" << setw(15) << setfill(' ') << "Количество" << "|" << setw(12) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl; for (int i = 0; i < currentSize; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } fclose(file1); break; } case 2: { for (int i = 0; i < N - 1; i++) { for (int j = 0; j < N - i - 1; j++) { if (tov[j].name < tov[j + 1].name) { tovar temp = tov[j]; tov[j] = tov[j + 1]; tov[j + 1] = temp; } } } for (int i = 0; i < N; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } break; } case 3: { for (int i = 0; i < N - 1; i++) { for (int j = 0; j < N - i - 1; j++) { if (tov[j].kol < tov[j + 1].kol) { tovar temp = tov[j]; tov[j] = tov[j + 1]; tov[j + 1] = temp; } } } for (int i = 0; i < N; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } break; } case 4: { cout << "Конец работы"; } case 5: { FILE* file1 = fopen("inf.txt", "ab"); cout << "Введите название товара: "; cin.ignore(); getline(cin, tov[N - 1].name); cout << "Введите количество: "; cin >> tov[N - 1].kol; cout << "Введите цену: "; cin >> tov[N - 1].price; cout << "Введите производителя: "; cin.ignore(); getline(cin, tov[N - 1].proiz); fwrite(&tov[N - 1], sizeof(tov[N - 1]), 1, file1); fclose(file1); break; } case 6: { file1 = fopen("inf.txt", "rb"); int currentSize = 0; while (fread(&tov[currentSize], sizeof(tov[currentSize]), 1, file1) == 1) { currentSize++; } cout << "|" << setw(44) << setfill('-') << "Таблицы" << setw(20) << "|" << endl; cout << "|" << setw(18) << setfill(' ') << "Наименование" << "|" << setw(15) << setfill(' ') << "Количество" << "|" << setw(12) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl; for (int i = 0; i < currentSize; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } break; } } } } C #include <iostream> #include <string> #include <stdio.h> #include <locale.h> #include <iomanip> #include <windows.h> using namespace std; const int N = 10; struct tovar { string name; float kol; float price; string proiz; }tov[N]; void quickSortByPrice(tovar* tov, int left, int right) { int i = left, j = right; tovar pivot = tov[(left + right) / 2]; while (i <= j) { while (tov[i].price < pivot.price) i++; while (tov[j].price > pivot.price) j--; if (i <= j) { swap(tov[i], tov[j]); i++; j--; } } if (left < j) quickSortByPrice(tov, left, j); if (i < right) quickSortByPrice(tov, i, right); } int main() { FILE* file1; SetConsoleCP(1251); SetConsoleOutputCP(1251); int A; while (true) { cout << "Что вы хотите сделать??" << endl; cout << "1. Отсортировать по цене" << endl; cout << "2. Отсортировать по имени" << endl; cout << "3. Отсортировать по Колличеству" << endl; cout << "4. Выход" << endl; cout << "5. Дополнить таблицу" << endl; cout << "6. Вывести таблицу" << endl; cin >> A; switch (A) { case 1: { file1 = fopen("inf.txt", "rb"); int currentSize = 0; while (fread(&tov[currentSize], sizeof(tov[currentSize]), 1, file1) == 1) { currentSize++; } quickSortByPrice(tov, 0, currentSize - 1); cout << "|" << setw(44) << setfill('-') << "Таблицы" << setw(20) << "|" << endl; cout << "|" << setw(18) << setfill(' ') << "Наименование" << "|" << setw(15) << setfill(' ') << "Количество" << "|" << setw(12) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl; for (int i = 0; i < currentSize; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } fclose(file1); break; } case 2: { for (int i = 0; i < N - 1; i++) { for (int j = 0; j < N - i - 1; j++) { if (tov[j].name < tov[j + 1].name) { tovar temp = tov[j]; tov[j] = tov[j + 1]; tov[j + 1] = temp; } } } for (int i = 0; i < N; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } break; } case 3: { for (int i = 0; i < N - 1; i++) { for (int j = 0; j < N - i - 1; j++) { if (tov[j].kol < tov[j + 1].kol) { tovar temp = tov[j]; tov[j] = tov[j + 1]; tov[j + 1] = temp; } } } for (int i = 0; i < N; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } break; } case 4: { cout << "Конец работы"; } case 5: { FILE* file1 = fopen("inf.txt", "ab"); cout << "Введите название товара: "; cin.ignore(); getline(cin, tov[N - 1].name); cout << "Введите количество: "; cin >> tov[N - 1].kol; cout << "Введите цену: "; cin >> tov[N - 1].price; cout << "Введите производителя: "; cin.ignore(); getline(cin, tov[N - 1].proiz); fwrite(&tov[N - 1], sizeof(tov[N - 1]), 1, file1); fclose(file1); break; } case 6: { file1 = fopen("inf.txt", "rb"); int currentSize = 0; while (fread(&tov[currentSize], sizeof(tov[currentSize]), 1, file1) == 1) { currentSize++; } cout << "|" << setw(44) << setfill('-') << "Таблицы" << setw(20) << "|" << endl; cout << "|" << setw(18) << setfill(' ') << "Наименование" << "|" << setw(15) << setfill(' ') << "Количество" << "|" << setw(12) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl; for (int i = 0; i < currentSize; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } break; } } } }
Могу предположить, что проблема возникает в функции fread, а именно из-за структуры tovar. Поля string name и string proiz делают недопустимым чтение и запись с помощью fread и fwrite. Копипаст со стек оверфлоу: Потому что в этих полях содержится, например, не сама строка, а указатель на нее. При записи вы сохраняете в файле какой-то адрес, потом считываете его - когда он уже давно не актуален, а при удалении вызов деструктора пытается удалить память по несуществующему адресу... Источник: https://ru.stackoverflow.com/questi...ушение-доступа-для-чтения-pnext-было-0x49b2dc (Не реклама)
Envil, Не работает. Выполни последовательность действий 5 - 4 - 6 и убедись в этом сам. (Тут, если по честному, то ты навернёшься еще на действии 4, потому что забыл там "return 0;" прописать, но не будем о грустном)
Envil, Насколько я помню плюсы, не получится так как ты сделал записать через fwrite структуру в которой есть string, потому что размер структуры не включает размеры строк которые в ней содержаться. Что бы в этом убедиться сократи программу до нужного минимума и проверь запись и чтение: #include <iostream> #include <string> #include <stdio.h> #include <iomanip> using namespace std; const int N = 10; struct tovar { string name; float kol; float price; string proiz; }tov[N]; void printTable(int size) { cout << "|" << setw(18) << setfill(' ') << "Наименование" << "|" << setw(15) << setfill(' ') << "Количество" << "|" << setw(12) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl; for (int i = 0; i < size; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } } int main() { while (true) { cout << "Что вы хотите сделать??" << endl; cout << "4. Выход" << endl; cout << "5. Дополнить таблицу" << endl; cout << "6. Вывести таблицу" << endl; int A; cin >> A; switch (A) { case 4: { cout << "Конец работы"; return 0; } case 5: { struct tovar newTovar; cout << "Введите название товара: "; cin.ignore(); getline(cin, newTovar.name); cout << "Введите количество: "; cin >> newTovar.kol; cout << "Введите цену: "; cin >> newTovar.price; cout << "Введите производителя: "; cin.ignore(); getline(cin, newTovar.proiz); FILE* file = fopen("inf.txt", "ab"); fwrite(&newTovar, sizeof(newTovar), 1, file); fclose(file); break; } case 6: { int currentSize = 0; FILE* file = fopen("inf.txt", "rb"); if (file){ while (fread(&tov[currentSize], sizeof(tov[currentSize]), 1, file) == 1) { currentSize++; } fclose(file); } printTable(currentSize); break; } } } } C #include <iostream> #include <string> #include <stdio.h> #include <iomanip> using namespace std; const int N = 10; struct tovar { string name; float kol; float price; string proiz; }tov[N]; void printTable(int size) { cout << "|" << setw(18) << setfill(' ') << "Наименование" << "|" << setw(15) << setfill(' ') << "Количество" << "|" << setw(12) << setfill(' ') << "Цена" << "|" << setw(15) << setfill(' ') << "Производитель" << "|" << endl; for (int i = 0; i < size; i++) { cout << "|" << setw(18) << setfill(' ') << tov[i].name << "|" << setw(15) << setfill(' ') << tov[i].kol << "|" << setw(12) << setfill(' ') << tov[i].price << "|" << setw(15) << setfill(' ') << tov[i].proiz << "|" << endl; } } int main() { while (true) { cout << "Что вы хотите сделать??" << endl; cout << "4. Выход" << endl; cout << "5. Дополнить таблицу" << endl; cout << "6. Вывести таблицу" << endl; int A; cin >> A; switch (A) { case 4: { cout << "Конец работы"; return 0; } case 5: { struct tovar newTovar; cout << "Введите название товара: "; cin.ignore(); getline(cin, newTovar.name); cout << "Введите количество: "; cin >> newTovar.kol; cout << "Введите цену: "; cin >> newTovar.price; cout << "Введите производителя: "; cin.ignore(); getline(cin, newTovar.proiz); FILE* file = fopen("inf.txt", "ab"); fwrite(&newTovar, sizeof(newTovar), 1, file); fclose(file); break; } case 6: { int currentSize = 0; FILE* file = fopen("inf.txt", "rb"); if (file){ while (fread(&tov[currentSize], sizeof(tov[currentSize]), 1, file) == 1) { currentSize++; } fclose(file); } printTable(currentSize); break; } } } }