From 3461d6dfc348b1814376669518f2dcbb114ab9c8 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 9 Oct 2016 12:29:14 +0300 Subject: Add rudimentary file reading and error checking --- main.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index b0846a7..4e0da53 100644 --- a/main.cpp +++ b/main.cpp @@ -1,12 +1,37 @@ #include +#include +#include + +std::string* read_file(const std::string& filename) { + std::ifstream file(filename); + + if (file.fail()) + return nullptr; + + std::string str; + std::string* file_contents = new std::string(); + while (std::getline(file, str)) { + *file_contents += str; + file_contents->push_back('\n'); + } + + return file_contents; +} int main(int argc, char** argv) { if (argc < 2) { - std::cout << "Please provide a file path."; + std::cout << "Please provide a file path.\n"; return 1; } std::string filepath(argv[1]); + std::string* contents = read_file(filepath); + if (contents == nullptr) { + std::cout << "Error reading file.\n"; + return 1; + } + + delete contents; return 0; } -- cgit v1.3