fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using std::cin;
  7. using std::cout;
  8. using std::endl;
  9.  
  10. void parse_as_char(std::istream& is)
  11. {
  12. char c1 = '#', c2 = '#', c3 = '#';
  13.  
  14. is.clear();
  15. is >> c1;
  16. cout << "(1) c1='" << c1 << "', is.good()=" << is.good() << ", is.eof()=" << is.eof() << ", is.fail()=" << is.fail() << endl;
  17. is >> c2;
  18. cout << "(2) c2='" << c2 << "', is.good()=" << is.good() << ", is.eof()=" << is.eof() << ", is.fail()=" << is.fail() << endl;
  19. is >> c3;
  20. cout << "(3) c3='" << c3 << "', is.good()=" << is.good() << ", is.eof()=" << is.eof() << ", is.fail()=" << is.fail() << endl;
  21. }
  22.  
  23. void parse_as_str(std::istream& is)
  24. {
  25. std::string s1 = "#", s2 = "#", s3 = "#";
  26.  
  27. is.clear();
  28. is >> s1;
  29. cout << "(1) s1=\"" << s1 << "\", is.good()=" << is.good() << ", is.eof()=" << is.eof() << ", is.fail()=" << is.fail() << endl;
  30. is >> s2;
  31. cout << "(2) s2=\"" << s2 << "\", is.good()=" << is.good() << ", is.eof()=" << is.eof() << ", is.fail()=" << is.fail() << endl;
  32. is >> s3;
  33. cout << "(3) s3=\"" << s3 << "\", is.good()=" << is.good() << ", is.eof()=" << is.eof() << ", is.fail()=" << is.fail() << endl;
  34. }
  35.  
  36. void test_parse_as_str(const char* szInput)
  37. {
  38. std::istringstream ist;
  39.  
  40. cout << __FUNCTION__ << ": Input=\"" << szInput << "\"" << endl;
  41. ist.str(szInput);
  42. parse_as_str(ist);
  43. cout << endl;
  44. }
  45.  
  46. void test_parse_as_char(const char* szInput)
  47. {
  48. std::istringstream ist;
  49.  
  50. cout << __FUNCTION__ << ": Input=\"" << szInput << "\"" << endl;
  51. ist.str(szInput);
  52. parse_as_char(ist);
  53. cout << endl;
  54. }
  55.  
  56. bool parse_as_string_with_common_logic(std::istream& is, std::string& s, bool& bErr)
  57. {
  58. s.clear();
  59. is >> s;
  60. if (!is) {
  61. if (is.eof()) {
  62. /*pass*/
  63. }
  64. else {
  65. bErr = true;
  66. }
  67. return false;
  68. }
  69. return true;
  70. }
  71.  
  72. void test_parse_as_string_with_common_logic(const char* szInput, bool& bErr)
  73. {
  74. std::string str;
  75.  
  76. cout << __FUNCTION__ << ": Input=\"" << szInput << "\"" << endl;
  77. std::istringstream ist;
  78. ist.str(szInput);
  79.  
  80. size_t i = (size_t)0;
  81. while (parse_as_string_with_common_logic(ist, str, bErr)) {
  82. i++;
  83. cout << "(" << i << ") str=\"" << str << "\", bErr=" << bErr << endl;
  84. }
  85. cout << "(" << "F" << ") str=\"" << str << "\", bErr=" << bErr << endl;
  86.  
  87. cout << endl;
  88. }
  89.  
  90. int main()
  91. {
  92. bool bErr = false;
  93.  
  94. // ケースA)
  95. cout << "Case A)" << endl;
  96. test_parse_as_char("A B"); // Bの後ろに空白無し --> 'B' を読んだ後の is >> c3 で初めて!is.good()かつis.eof()成立(getc()と同じ挙動)
  97.  
  98. // ケースA')
  99. cout << "Case A')" << endl;
  100. test_parse_as_char("A B "); // Bの後ろに空白有り --> (同上)
  101.  
  102. // ケースB)
  103. cout << "Case B)" << endl;
  104. test_parse_as_str("A B"); // Bの後ろに空白無し --> "B" を読んだ時点で!is.good()かつis.eof()成立
  105.  
  106. // ケースC)
  107. cout << "Case C)" << endl;
  108. test_parse_as_str("A B "); // Bの後ろに空白有り --> "B" を読んだ後の is >> s3 で初めて!is.good()かつis.eof()成立(getc()と同じ挙動)
  109.  
  110. // 共通判定ロジックによるCase Bの読み取り
  111. cout << "Case B with common logic:" << endl;
  112. test_parse_as_string_with_common_logic("A B", bErr);
  113.  
  114. // 共通判定ロジックによるCase Cの読み取り
  115. cout << "Case C with common logic:" << endl;
  116. test_parse_as_string_with_common_logic("A B ", bErr);
  117. }
  118.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Case A)
test_parse_as_char: Input="A B"
(1) c1='A', is.good()=1, is.eof()=0, is.fail()=0
(2) c2='B', is.good()=1, is.eof()=0, is.fail()=0
(3) c3='#', is.good()=0, is.eof()=1, is.fail()=1

Case A')
test_parse_as_char: Input="A B "
(1) c1='A', is.good()=1, is.eof()=0, is.fail()=0
(2) c2='B', is.good()=1, is.eof()=0, is.fail()=0
(3) c3='#', is.good()=0, is.eof()=1, is.fail()=1

Case B)
test_parse_as_str: Input="A B"
(1) s1="A", is.good()=1, is.eof()=0, is.fail()=0
(2) s2="B", is.good()=0, is.eof()=1, is.fail()=0
(3) s3="#", is.good()=0, is.eof()=1, is.fail()=1

Case C)
test_parse_as_str: Input="A B "
(1) s1="A", is.good()=1, is.eof()=0, is.fail()=0
(2) s2="B", is.good()=1, is.eof()=0, is.fail()=0
(3) s3="#", is.good()=0, is.eof()=1, is.fail()=1

Case B with common logic:
test_parse_as_string_with_common_logic: Input="A B"
(1) str="A", bErr=0
(2) str="B", bErr=0
(F) str="", bErr=0

Case C with common logic:
test_parse_as_string_with_common_logic: Input="A B "
(1) str="A", bErr=0
(2) str="B", bErr=0
(F) str="", bErr=0