From 19419d86cb88981df7973b08a0da6373abfba566 Mon Sep 17 00:00:00 2001 From: Oskar Date: Sun, 6 Oct 2024 14:09:53 +0200 Subject: more exercises --- 8p7.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 8p7.cpp (limited to '8p7.cpp') diff --git a/8p7.cpp b/8p7.cpp new file mode 100644 index 0000000..93b9c50 --- /dev/null +++ b/8p7.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "sales_data.hpp" + +/* + * + * 8.7 + * + * + */ + +int main (int argc, char **argv) { + + if(argc < 3 || argc > 4) { + return -1; + } + + std::ifstream file(argv[1]); // open first file to read + std::ifstream out_file_chk(argv[2], std::ifstream::ate); // Open 2nd file to check if it exists + if(!out_file_chk.is_open()) { // If it does not exist create it + std::cout << "File created." << std::endl; + std::ofstream create_file(argv[2]); + if(create_file.fail()) { return -1; } + create_file.close(); + } + + if(file.fail()) { // check if first file failed to open + return -1; + } + + if(out_file_chk.tellg() > 0) { // Check if the 2nd file had any data + std::cerr << "This file has data" << std::endl; + return -1; + } + + out_file_chk.close(); + std::ofstream out_file(argv[2]); // Finally open for writing + SalesData total; + if (read(file, total)) { + SalesData trans; + while(read(file, trans)) { + if (total.isbn() == trans.isbn()) + total.combine(trans); + else { + print(out_file, total) << std::endl; + total = trans; + } + } + print(out_file, total) << std::endl; + } else { + std::cerr << "No data?!" << std::endl; + } +} -- cgit v1.2.3