Advertisement
Guest User

Untitled

a guest
May 25th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. class CharacterReader {
  6. public:
  7. virtual char NextCharacter() = 0;
  8. virtual void Close() = 0;
  9. };
  10.  
  11. class FastCharacterReaderImpl final : public CharacterReader {
  12.  
  13. public:
  14. FastCharacterReaderImpl();
  15. ~FastCharacterReaderImpl();
  16.  
  17. char NextCharacter() override;
  18. void Close() override;
  19.  
  20. unsigned int FileLength();
  21.  
  22. private:
  23. std::ifstream is_;
  24. unsigned int file_length_;
  25. };
  26.  
  27. FastCharacterReaderImpl::FastCharacterReaderImpl() {
  28. is_.open("testtext2.txt");
  29. if (is_) {
  30. is_.seekg (0, is_.end);
  31. file_length_ = is_.tellg();
  32. is_.seekg (0, is_.beg);
  33. }
  34. }
  35.  
  36. FastCharacterReaderImpl::~FastCharacterReaderImpl() {
  37. if (is_)
  38. is_.close();
  39. }
  40.  
  41. unsigned int FastCharacterReaderImpl::FileLength() {
  42. return file_length_;
  43. }
  44.  
  45. char FastCharacterReaderImpl::NextCharacter() {
  46. char c;
  47. if (!is_.eof())
  48. is_.get(c);
  49. return c;
  50. }
  51.  
  52. void FastCharacterReaderImpl::Close() {
  53. is_.close();
  54. }
  55.  
  56. int main ()
  57. {
  58. FastCharacterReaderImpl obj;
  59.  
  60. for (auto i = 0; i < obj.FileLength(); i++)
  61. std::cout << obj.NextCharacter();
  62.  
  63. obj.Close();
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement