diff options
author | Oskar <[email protected]> | 2024-10-06 14:09:53 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-10-06 14:09:53 +0200 |
commit | 19419d86cb88981df7973b08a0da6373abfba566 (patch) | |
tree | 6f10b0908814dd9171b1abde88a59d78946e3d89 /8p7.cpp | |
parent | 6d33273f62fb78c481babeda6317d0138974ff02 (diff) |
more exercises
Diffstat (limited to '8p7.cpp')
-rw-r--r-- | 8p7.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -0,0 +1,53 @@ +#include <iostream> +#include <fstream> +#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; + } +} |