Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- class FileReader
- {
- protected:
- std::string path;
- std::ifstream inf;
- public:
- FileReader(std::string pathP) : path{ pathP }, inf{pathP}
- {
- if (!inf) { throw std::runtime_error("I/O Error!"); }
- }
- virtual void display(const char byte)const
- {
- std::cout << byte;
- }
- const FileReader& show()
- {
- while (!inf.eof())
- {
- display(inf.get());
- }
- return *this;
- }
- virtual ~FileReader() { inf.close(); }
- };
- class FileReaderASCII : public FileReader
- {
- public:
- FileReaderASCII(std::string pathP) : FileReader{ pathP } {}
- void display(const char byte) const override
- {
- if (byte > 0)
- {
- std::cout << static_cast<unsigned int>(byte) << ' ';
- }
- }
- };
- class FileReaderBIN : public FileReader
- {
- public:
- FileReaderBIN(std::string pathP) : FileReader{ pathP } {}
- void display(const char byte) const override
- {
- if (byte > 0)
- {
- for (int bit{ 7 }, mask{1}; bit >= 0; --bit, mask = (1 << bit))
- {
- std::cout << static_cast<bool>(byte & mask);
- }
- std::cout << ' ';
- }
- }
- };
- int main()
- {
- std::unique_ptr<FileReader> readers[3];
- // CHANGE THIS TO REAL FILENAME PATH!!!
- std::string fileName{ "../ConsoleApplication8/ConsoleApplication8.cpp" };
- try
- {
- readers[0] = std::make_unique<FileReader>(fileName);
- readers[1] = std::make_unique<FileReaderASCII>(fileName);
- readers[2] = std::make_unique<FileReaderBIN>(fileName);
- for (const auto& reader : readers)
- {
- std::cout << "\n####### Output for " << typeid(*reader).name() << "#######\n";
- reader->show();
- }
- }
- catch(std::exception& ex)
- {
- std::cout << "ERROR: " << ex.what() << '\n';
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement