diff options
| author | Jan Tuomi <jan-sebastian.tuomi@aalto.fi> | 2016-10-09 12:29:14 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan-sebastian.tuomi@aalto.fi> | 2016-10-09 14:50:24 +0300 |
| commit | 3461d6dfc348b1814376669518f2dcbb114ab9c8 (patch) | |
| tree | 32715ffd06d227019d02b35e9e327c3f577db954 | |
| parent | a16e59737d6d1cd89cd870f872331ec806e22c78 (diff) | |
Add rudimentary file reading and error checking
| -rw-r--r-- | main.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -1,12 +1,37 @@ #include <iostream> +#include <fstream> +#include <string> + +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; } |
