Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- template <class T>
- std::string check_status(std::basic_ios<T>& stream)
- {
- if (stream.good()) {
- return "good";
- } else {
- std::string msg;
- if (stream.rdstate() & std::ios_base::failbit) {
- msg += "failbit";
- }
- if (stream.rdstate() & std::ios_base::eofbit) {
- msg += msg.empty() ? "eofbit" : ", eofbit";
- }
- if (stream.rdstate() & std::ios_base::badbit) {
- msg += msg.empty() ? "badbit" : ", badbit";
- }
- return msg + " set";
- }
- }
- int main(int argc, char const* argv[])
- {
- std::ofstream ofs("test.in", std::ios::out);
- ofs << "hello";
- std::fstream ifs;
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- char ch = ifs.get();
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- printf(" -- ch = %x\n", (unsigned)ch);
- ifs.open("test.in", std::ios::in);
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- ifs.get();
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- printf(" -- ch = %x\n", (unsigned)ch);
- int num = 0;
- ifs >> num;
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- printf(" -- num = %d\n", num);
- ifs.clear();
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- ifs.close();
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- ch = ifs.get();
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- printf(" -- ch = %x\n", (unsigned)ch);
- ifs.open("test.in", std::ios::in);
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- std::string text;
- ifs >> text;
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- printf(" -- text = %s\n", text.c_str());
- ifs.clear();
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- ifs.put('0');
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- printf(" -- text = %s\n", text.c_str());
- ifs.close();
- printf("%d\t| %s\n", __LINE__, check_status(ifs).c_str());
- return 0;
- }
- #if 0
- 35 | good
- 38 | failbit, eofbit set
- -- ch = ffffffff
- 42 | good
- 45 | failbit, eofbit set
- -- ch = ffffffff
- 50 | failbit, eofbit set
- -- num = 0
- 54 | good
- 57 | good
- 60 | failbit, eofbit set
- -- ch = ffffffff
- 64 | good
- 68 | failbit, eofbit set
- -- text =
- 72 | good
- 75 | badbit set
- -- text =
- 79 | badbit set
- #endif
Add Comment
Please, Sign In to add comment