I can not understand why the variables " choke " after the introduction of the second and so on. write ... I do not know how to fix it (

Code:

#include "stdafx.h" #include <iostream> #include <fstream> #include <cstring> #include<clocale> using namespace std; typedef unsigned int vdt; // изменяемый тип данных (vdt) struct fileDB { char nameF[20] = "\0"; // название файла (nameF, c) char typeF[20] = "\0"; // тип файла (typeF, c) char locationF[20] = "\0"; // расположение файла (locationF, c) char attributeF[20] = "\0"; // атрибут (attributeF, c) char dateF[20] = "\0"; // дата создания (dateF, c) vdt sizeF = 0; // размер файла (sizeF, vdt) }; int main() { setlocale(LC_CTYPE, ""); fileDB *files; files = new fileDB; fstream *fileDB; fileDB = new fstream(); int menu_flag = -1, case3_flag; char nameFS[20] = "\0", typeFS[20] = "\0", dateFS[20] = "\0", attributeFS[20] = "\0", locationFS[20] = "\0"; // перменные символьного типа для сравнения со структурой int sizeFS = 0; // переменные целочисленного типа для сравнения со структурой bool have; // если отстуствует искомый процессор, то флаг становится false и выводится: "Совпадений нет." while (menu_flag) { cout << "1 - Создать новый файл\n"; cout << "2 - Прочитать файл\n"; cout << "3 - Поиск\n"; cout << "0 - Выход\n"; cout << "Ваш ввод: "; cin >> menu_flag; switch (menu_flag) { case 1: { //создание файла fileDB->open("file.db", ios::binary | ios::app); char case1_flag[4] = "yes"; // перменная для запроса повторного ввода записи while (strcmp(case1_flag, "no") != 0) // цикл для заполнения структуры в файл { cout << "Введите название файла: "; cin >> files->nameF; cout << "Введите тип файла: "; cin >> files->typeF; cout << "Введите расположение файла: "; cin >> files->locationF; cout << "Введите атрибут файла(A, B, C,...): "; cin >> files->attributeF; cout << "Введите дата создания файла: "; cin >> files->dateF; cout << "Введите размер файла в МБ: "; cin >> files->sizeF; fileDB->write((char*)files, sizeof(fileDB)); cout << "Добавить еще запись в файл yes/no?: "; //запрос повтороного ввода записи в файл fflush(stdin); cin >> case1_flag; } } fileDB->close(); break; case 2: { fileDB->open("file.db", ios::binary | ios::in); if (!fileDB->is_open()) { cout << "\nФайл не найден\n\n"; break; } fileDB->close(); fileDB->open("file.db", ios::binary | ios::in); while (fileDB->read((char*)files, sizeof(fileDB))) { cout << "\n|================================|\n"; cout << "Название файла: " << files->nameF << endl; cout << "Тип файла: " << files->typeF << endl; cout << "Расположение файла: " << files->locationF << endl; cout << "Атрибут файла: " << files->attributeF << endl; cout << "Дата создания файла: " << files->dateF << endl; cout << "Размер файла: " << files->sizeF << endl; cout << "|================================|\n\n"; } } fileDB->close(); break; case 3: { fileDB->open("file.db", ios::binary | ios::in); if (!fileDB->is_open()) { cout << "\nФайл не найден\n\n"; break; } fileDB->close(); case3_flag = -1; while (case3_flag) { cout << "\n1 - Поиск по названию файла\n"; cout << "2 - Поиск по типу файла\n"; cout << "3 - Поиск по расположению файла\n"; cout << "4 - Поиск по атрибуту файла\n"; cout << "5 - Поиск по дате создания файла\n"; cout << "6 - Поиск по размеру файла\n"; cout << "0 - Назад\n"; cout << "Ваш ввод: "; cin >> case3_flag; switch (case3_flag) { case 1: { fileDB->open("file.db", ios::binary | ios::in); have = false; cout << "Введите название файла: "; cin >> nameFS; while (fileDB->read((char*)files, sizeof(fileDB))) { if (strcmp(nameFS, files->nameF) == 0) { cout << "\n|================================|\n"; cout << "Название файла: " << files->nameF << endl; cout << "Тип файла: " << files->typeF << endl; cout << "Расположение файла: " << files->locationF << endl; cout << "Атрибут файла: " << files->attributeF << endl; cout << "Дата создания файла: " << files->dateF << endl; cout << "Размер файла: " << files->sizeF << endl; cout << "|================================|\n\n"; have = true; } } if (!have) cout << "\nСовпадений нет\n"; fileDB->close(); } break; case 2: { fileDB->open("file.db", ios::binary | ios::in); have = false; cout << "Введите тип файла: "; cin >> typeFS; while (fileDB->read((char*)files, sizeof(fileDB))) { if (strcmp(typeFS, files->typeF) == 0) { cout << "\n|================================|\n"; cout << "Название файла: " << files->nameF << endl; cout << "Тип файла: " << files->typeF << endl; cout << "Расположение файла: " << files->locationF << endl; cout << "Атрибут файла: " << files->attributeF << endl; cout << "Дата создания файла: " << files->dateF << endl; cout << "Размер файла: " << files->sizeF << endl; cout << "|================================|\n\n"; have = true; } } if (!have) cout << "\nСовпадений нет\n"; fileDB->close(); } break; case 3: { fileDB->open("file.db", ios::binary | ios::in); have = false; cout << "Введите расположение файла:"; cin >> locationFS; while (fileDB->read((char*)files, sizeof(fileDB))) { if (strcmp(locationFS, files->locationF) == 0) { cout << "\n|================================|\n"; cout << "Название файла: " << files->nameF << endl; cout << "Тип файла: " << files->typeF << endl; cout << "Расположение файла: " << files->locationF << endl; cout << "Атрибут файла: " << files->attributeF << endl; cout << "Дата создания файла: " << files->dateF << endl; cout << "Размер файла: " << files->sizeF << endl; cout << "|================================|\n\n"; have = true; } } if (!have) cout << "\nСовпадений нет\n"; fileDB->close(); } break; case 4: { fileDB->open("file.db", ios::binary | ios::in); have = false; cout << "Введите атрибут файла: "; cin >> attributeFS; while (fileDB->read((char*)files, sizeof(fileDB))) { if (strcmp(attributeFS, files->attributeF) == 0) { cout << "\n|================================|\n"; cout << "Название файла: " << files->nameF << endl; cout << "Тип файла: " << files->typeF << endl; cout << "Расположение файла: " << files->locationF << endl; cout << "Атрибут файла: " << files->attributeF << endl; cout << "Дата создания файла: " << files->dateF << endl; cout << "Размер файла: " << files->sizeF << endl; cout << "|================================|\n\n"; have = true; } } if (!have) cout << "\nСовпадений нет\n"; fileDB->close(); } break; case 5: { fileDB->open("file.db", ios::binary | ios::in); have = false; cout << "Введите дату создания файла: "; cin >> dateFS; while (fileDB->read((char*)files, sizeof(fileDB))) { if (strcmp(dateFS, files->dateF) == 0) { cout << "\n|================================|\n"; cout << "Название файла: " << files->nameF << endl; cout << "Тип файла: " << files->typeF << endl; cout << "Расположение файла: " << files->locationF << endl; cout << "Атрибут файла: " << files->attributeF << endl; cout << "Дата создания файла: " << files->dateF << endl; cout << "Размер файла: " << files->sizeF << endl; cout << "|================================|\n\n"; have = true; } } if (!have) cout << "\nСовпадений нет\n"; fileDB->close(); } break; case 6: { fileDB->open("file.db", ios::binary | ios::in); have = false; cout << "Введите размер файла: "; cin >> sizeFS; while (fileDB->read((char*)files, sizeof(fileDB))) { if (sizeFS >= files->sizeF) { cout << "\n|================================|\n"; cout << "Название файла: " << files->nameF << endl; cout << "Тип файла: " << files->typeF << endl; cout << "Расположение файла: " << files->locationF << endl; cout << "Атрибут файла: " << files->attributeF << endl; cout << "Дата создания файла: " << files->dateF << endl; cout << "Размер файла: " << files->sizeF << endl; cout << "|================================|\n\n"; have = true; } } if (!have) cout << "\nСовпадений нет\n"; fileDB->close(); } break; default:break; }//end of switch } fileDB->close(); } break; case 0: return 0; // выход из программы default:break; } } return 0; } 

Conclusion:

 1 - Создать новый файл 2 - Прочитать файл 3 - Поиск 0 - Выход Ваш ввод: 1 Введите название файла: test Введите тип файла: txt Введите расположение файла: c:\ Введите атрибут файла(A, B, C,...): b Введите дата создания файла: 12.12.12 Введите размер файла в МБ: 12 Добавить еще запись в файл yes/no?: y Введите название файла: qwe Введите тип файла: asd Введите расположение файла: c:\ Введите атрибут файла(A, B, C,...): a Введите дата создания файла: 13.13.12 Введите размер файла в МБ: 14 Добавить еще запись в файл yes/no?: no 1 - Создать новый файл 2 - Прочитать файл 3 - Поиск 0 - Выход Ваш ввод: 2 |================================| Название файла: test Тип файла: asd Расположение файла: c:\ Атрибут файла: a Дата создания файла: 13.13.12 Размер файла: 14 |================================| |================================| Название файла: qwe Тип файла: asd Расположение файла: c:\ Атрибут файла: a Дата создания файла: 13.13.12 Размер файла: 14 |================================| 1 - Создать новый файл 2 - Прочитать файл 3 - Поиск 0 - Выход Ваш ввод:... 

And so, what happens when I create a new file: I fill in all the parameters, then I want to surprise the result, but I see that the previous entries were rewritten to a new one and the “name” does not change. And how it should be: when adding a new record, the record should be added, and the old ones should not be changed.

Closed due to the fact that the essence of the question is incomprehensible to the participants Harry , andreymal , cheops , Edward , user194374 Jan 4 'at 17:51 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    Epic Header) - KAGG Design
  • one
    Brrr ... What does "choke" mean? What do you want to receive, and what is wrong? - Harry
  • so that's what char nameF [20] = "/ 0"; Isn't that it? "\ 0" is correct? - maint
  • @maint yes you are right, even I didn’t go there \ - MiT
  • @Harry look at the output please, especially what I enter) In fact, when I add a new record, the old records correspond with the new one, but the names remain the same. - MiT

1 answer 1

You have a struct fileDB type and a local variable fstream *fileDB; with the same name. Respectively when you write fileDB->write((char*)files, sizeof(fileDB)); or read fileDB->read((char*)files, sizeof(fileDB) number of bytes corresponding to the size of a local variable will be written, that is, only 4 or 8 bytes. To avoid such problems you should: first give names with this calculation, so that identifiers for types and variables never overlap (for example, a different case and / or the prefix СfileDB is a type and file_db is a variable), secondly, if possible, apply sizeof directly to a variable ( sizeof(*files) is the size of the structure pointed to by files ), thirdly, check the number of actually recorded / read byte.

In addition, try to avoid copy-paste, otherwise your code is incredibly bloated. For example, on the move you can put in a separate method the code for printing the contents of the record, otherwise it is now duplicated 7 times.

Initialization of the structure can also be simplified, "\0" not needed:

 struct fileDB { char nameF[20]{}; // название файла (nameF, c) char typeF[20]{}; // тип файла (typeF, c) char locationF[20]{}; // расположение файла (locationF, c) char attributeF[20]{}; // атрибут (attributeF, c) char dateF[20]{}; // дата создания (dateF, c) vdt sizeF{}; // размер файла (sizeF, vdt) }; 

or remove the initialization of individual fields altogether and initialize with zeros when creating the object files = new fileDB{};

  • Thank you, I understood my mistakes, corrected) - MiT