From aae58d4bfafd8fbed33c98b64bada51dcbd32744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=91=D0=BE=D0=B3=D0=B4=D0=B0?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2?= Date: Sat, 6 Nov 2021 18:52:42 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D0=BE=20Functions.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\320\264\320\2671_2/Functions.cpp" | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git "a/\320\264\320\2671_2/Functions.cpp" "b/\320\264\320\2671_2/Functions.cpp" index 01aa417..ad7635b 100644 --- "a/\320\264\320\2671_2/Functions.cpp" +++ "b/\320\264\320\2671_2/Functions.cpp" @@ -10,6 +10,10 @@ void Primary_input(const std::string& filename) { Product elem; while (Indicator) { elem.NewProduct(); + + //!!! Неверная работа с бинарными файлами - с ними нужно использовать методы read и write + //!!! читать/записывать целиком объект Product, а не его поля. + elem.Output_in_file(file); std::cout <> Indicator; @@ -33,7 +37,12 @@ void Delete_an_record(const std::string& filename) { std::cout << "Enter the number of the record to delete:"; std::cin >> num; List_Product records; + + //!!! Неверная работа с бинарными файлами - с ними нужно использовать методы read и write + //!!! читать/записывать целиком объект Product, а не его поля. records.Output_from_file(file); + + file.close(); records.Output_map_Product().erase(num); std::ofstream file(filename + ".bin", std::ios::binary); @@ -53,6 +62,9 @@ void Replace_record(const std::string& filename) { std::cout << "Enter the record number to replace:"; std::cin >> num; List_Product records; + + //!!! Неверная работа с бинарными файлами - с ними нужно использовать методы read и write + //!!! читать/записывать целиком объект Product, а не его поля. records.Output_from_file(file); file.close(); Product record; @@ -102,6 +114,9 @@ void Search_for_records(const std::string& filename) { break; } std::ofstream file(Newfilename + ".bin", std::ios::binary); + + //!!! Неверная работа с бинарными файлами - с ними нужно использовать методы read и write + //!!! читать/записывать целиком объект Product, а не его поля. records.Output_in_file(file); std::cout << std::endl; } @@ -110,16 +125,24 @@ void Search_for_records(const std::string& filename) { void Convert_to_a_text_file(const std::string& filename) { std::ifstream file_bin(filename + ".bin", std::ios::binary); - std::ofstream file_txt(filename + ".txt", std::ios::binary); + + + std::ofstream file_txt(filename + ".txt", std::ios::binary); //!!! Зачем тут std::ios::binary + + + if (!file_bin || !file_txt) { std::cout << "Error: the file did not open!" << std::endl << std::endl; } else { + //!!! Шарина должна задаваться манипуляторами file_txt << "Warehouse number " << "Product code " << "Product name " << "Date of receipt " << "Shelf life in days " << "Number of product units " << "Unit price" << std::endl; List_Product records; records.Output_from_file(file_bin); int total_shelf_life = 0, total_quantity = 0, total__price = 0; - for (auto& elem : records.Output_map_Product()) { + for (auto& elem : records.Output_map_Product()) { //!!! elem изменяется в цикле. ПОЧЕМУ НЕ ИСПОЛЬЗУЕТЕ const ТАМ ГДЕ НАДО? + + //!!! Форматировать надо манипуляторыми (с заданием шырины вывода) file_txt << elem.second.Output_number_warehouse() << "\t\t\t" << elem.second.Output_code() << "\t\t\t" << elem.second.Output_name() << "\t\t\t" << elem.second.Output_date()[0] << "." << elem.second.Output_date()[1] << "." << elem.second.Output_date()[2] << "\t\t\t" << elem.second.Output_shelf_life() << "\t\t\t" << elem.second.Output_quantity() << "\t\t\t" << elem.second.Output_price() << std::endl; -- GitLab