From 6ba133519b3443afc6128884cedcdad2bba13a00 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 20 Aug 2020 01:44:08 +0530 Subject: [PATCH] Error Handdling of Files --- machine_learning/neural_network.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/machine_learning/neural_network.cpp b/machine_learning/neural_network.cpp index aad469b14..fc10d2e10 100644 --- a/machine_learning/neural_network.cpp +++ b/machine_learning/neural_network.cpp @@ -390,6 +390,11 @@ namespace machine_learning { const int &slip_lines = 1) { std::ifstream in_file; // Ifstream to read file in_file.open(file_name.c_str(), std::ios::in); // Open file + // If there is any problem in opening file + if(!in_file.is_open()) { + std::cerr << "ERROR: Unable to open file: "<< file_name << std::endl; + exit(EXIT_FAILURE); + } std::vector >> X, Y; // To store X and Y std::string line; // To store each line // Skip lines @@ -645,6 +650,11 @@ namespace machine_learning { std::ofstream out_file; // Ofstream to write in file // Open file in out|trunc mode out_file.open(file_name.c_str(), std::ofstream::out | std::ofstream::trunc); + // If there is any problem in opening file + if(!out_file.is_open()) { + std::cerr << "ERROR: Unable to open file: "<< file_name << std::endl; + exit(EXIT_FAILURE); + } /** Format in which model is saved: @@ -711,6 +721,11 @@ namespace machine_learning { NeuralNetwork load_model (const std::string &file_name) { std::ifstream in_file; // Ifstream to read file in_file.open(file_name.c_str()); // Openinig file + // If there is any problem in opening file + if(!in_file.is_open()) { + std::cerr << "ERROR: Unable to open file: "<< file_name << std::endl; + exit(EXIT_FAILURE); + } std::vector > config; // To store config std::vector >> kernals; // To store pretrained kernals // Loading model from saved file format