Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. class Exception : public std::exception
  2. {
  3. public:
  4.     enum class State { FILE_DOES_NOT_EXISTS = 1, FILE_IS_NOT_OPEN = 2 };
  5.  
  6.     explicit Exception(State state) : state(state) {}
  7.  
  8.     const char* what() const noexcept override { return state_to_string(); }
  9.  
  10. private:
  11.     const char* state_to_string() const
  12.     {
  13.         switch (state)
  14.         {
  15.         case State::FILE_DOES_NOT_EXISTS:
  16.             return "File does not exists";
  17.         case State::FILE_IS_NOT_OPEN:
  18.             return "File is not open";
  19.         }
  20.     }
  21.  
  22. private:
  23.     State state;
  24. };
  25.  
  26. list read_file(const fs::path& file_path)
  27. {
  28.     if(!fs::exists(file_path))
  29.         throw Exception{Exception::State::FILE_DOES_NOT_EXISTS};
  30.  
  31.     std::ifstream infile{file_path.string()};
  32.  
  33.     if(!infile.is_open())
  34.         throw Exception{Exception::State::FILE_IS_NOT_OPEN};
  35.  
  36.     list data{};
  37.  
  38.     std::string number{""};
  39.     while(std::getline(infile, number, ','))
  40.         data.push_back(std::stoi(number));
  41.  
  42.     return data;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement