Taraxacum

Stream Status

Mar 20th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. template <class T>
  6. std::string check_status(std::basic_ios<T>& stream)
  7. {
  8.     if (stream.good()) {
  9.         return "good";
  10.     } else {
  11.         std::string msg;
  12.  
  13.         if (stream.rdstate() & std::ios_base::failbit) {
  14.             msg += "failbit";
  15.         }
  16.  
  17.         if (stream.rdstate() & std::ios_base::eofbit) {
  18.             msg += msg.empty() ? "eofbit" : ", eofbit";
  19.         }
  20.  
  21.         if (stream.rdstate() & std::ios_base::badbit) {
  22.             msg += msg.empty() ? "badbit" : ", badbit";
  23.         }
  24.  
  25.         return msg + " set";
  26.     }
  27. }
  28.  
  29. int main(int argc, char const* argv[])
  30. {
  31.     std::ofstream ofs("test.in", std::ios::out);
  32.     ofs << "hello";
  33.  
  34.     std::fstream ifs;
  35.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  36.  
  37.     char ch = ifs.get();
  38.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  39.     printf("  --  ch = %x\n", (unsigned)ch);
  40.  
  41.     ifs.open("test.in", std::ios::in);
  42.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  43.  
  44.     ifs.get();
  45.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  46.     printf("  --  ch = %x\n", (unsigned)ch);
  47.  
  48.     int num = 0;
  49.     ifs >> num;
  50.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  51.     printf("  --  num = %d\n", num);
  52.  
  53.     ifs.clear();
  54.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  55.  
  56.     ifs.close();
  57.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  58.  
  59.     ch = ifs.get();
  60.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  61.     printf("  --  ch = %x\n", (unsigned)ch);
  62.  
  63.     ifs.open("test.in", std::ios::in);
  64.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  65.  
  66.     std::string text;
  67.     ifs >> text;
  68.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  69.     printf("  --  text = %s\n", text.c_str());
  70.  
  71.     ifs.clear();
  72.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  73.  
  74.     ifs.put('0');
  75.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  76.     printf("  --  text = %s\n", text.c_str());
  77.  
  78.     ifs.close();
  79.     printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
  80.  
  81.     return 0;
  82. }
  83.  
  84. #if 0
  85. 35      | good
  86. 38      | failbit, eofbit set
  87.   --  ch = ffffffff
  88. 42      | good
  89. 45      | failbit, eofbit set
  90.   --  ch = ffffffff
  91. 50      | failbit, eofbit set
  92.   --  num = 0
  93. 54      | good
  94. 57      | good
  95. 60      | failbit, eofbit set
  96.   --  ch = ffffffff
  97. 64      | good
  98. 68      | failbit, eofbit set
  99.   --  text =
  100. 72      | good
  101. 75      | badbit set
  102.   --  text =
  103. 79      | badbit set
  104. #endif
Add Comment
Please, Sign In to add comment