Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- class CharacterReader {
- public:
- virtual char NextCharacter() = 0;
- virtual void Close() = 0;
- };
- class FastCharacterReaderImpl final : public CharacterReader {
- public:
- FastCharacterReaderImpl();
- ~FastCharacterReaderImpl();
- char NextCharacter() override;
- void Close() override;
- unsigned int FileLength();
- private:
- std::ifstream is_;
- unsigned int file_length_;
- };
- FastCharacterReaderImpl::FastCharacterReaderImpl() {
- is_.open("testtext2.txt");
- if (is_) {
- is_.seekg (0, is_.end);
- file_length_ = is_.tellg();
- is_.seekg (0, is_.beg);
- }
- }
- FastCharacterReaderImpl::~FastCharacterReaderImpl() {
- if (is_)
- is_.close();
- }
- unsigned int FastCharacterReaderImpl::FileLength() {
- return file_length_;
- }
- char FastCharacterReaderImpl::NextCharacter() {
- char c;
- if (!is_.eof())
- is_.get(c);
- return c;
- }
- void FastCharacterReaderImpl::Close() {
- is_.close();
- }
- int main ()
- {
- FastCharacterReaderImpl obj;
- for (auto i = 0; i < obj.FileLength(); i++)
- std::cout << obj.NextCharacter();
- obj.Close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement