{"action":"created","issue":{"url":"https://api.github.com/repos/Ocxs/Cpp-Primer/issues/11","labels_url":"https://api.github.com/repos/Ocxs/Cpp-Primer/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/Ocxs/Cpp-Primer/issues/11/comments","events_url":"https://api.github.com/repos/Ocxs/Cpp-Primer/issues/11/events","html_url":"https://github.com/Ocxs/Cpp-Primer/issues/11","id":63543901,"number":11,"title":"chapter8_question2_book(what does rdstate() return value means?)","user":{"login":"Ocxs","id":11175797,"avatar_url":"https://avatars.githubusercontent.com/u/11175797?v=3","gravatar_id":"","url":"https://api.github.com/users/Ocxs","html_url":"https://github.com/Ocxs","followers_url":"https://api.github.com/users/Ocxs/followers","following_url":"https://api.github.com/users/Ocxs/following{/other_user}","gists_url":"https://api.github.com/users/Ocxs/gists{/gist_id}","starred_url":"https://api.github.com/users/Ocxs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Ocxs/subscriptions","organizations_url":"https://api.github.com/users/Ocxs/orgs","repos_url":"https://api.github.com/users/Ocxs/repos","events_url":"https://api.github.com/users/Ocxs/events{/privacy}","received_events_url":"https://api.github.com/users/Ocxs/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":16,"created_at":"2015-03-22T15:30:59Z","updated_at":"2015-03-23T14:34:59Z","closed_at":null,"body":"@pezy \r\nI asked [this queation](http://stackoverflow.com/questions/29058612/what-does-rdstate-return-value-means) in SO.\r\n尽管大牛回答了，依然不是太明白，再此以中文重新描述一下。\r\n```\r\nistream& Read(istream &is)\r\n{\r\n    std::string buf;\r\n    while (is >> buf)       \r\n    {   \r\n        cout << is.eofbit << \" \" << is.failbit << \" \" << is.badbit << endl;\r\n        cout << is.rdstate() << endl;\r\n        cout << buf << endl;\r\n    }\r\n    cout << is.eofbit << \" \" << is.failbit << \" \" << is.badbit << endl;\r\n    cout << is.rdstate() << endl;\r\n    is.clear();\r\n    cout << is.eofbit << \" \" << is.failbit << \" \" << is.badbit << endl;\r\n    cout << is.rdstate() << endl;\r\n    return is;\r\n}\r\n```\r\n1.\r\n我如果输入普通的字符串，输出是1 2 4 0，对应eofbit   ailbit   badbit   rdstate() \r\n如果我键入  CTRL+Z (windows)，输出1 2 4 3，说明当前状态有变化，那么这个返回的3代表什么意思呢？是eofbit   ailbit   badbit 变化的个数？比如变化两个，就返回2?\r\n\r\n2.我输入CTRL+Z，按说eofbit和failbit 会变化吧？书上都说了\r\n> Reaching end-of-file sets both eofbit and failbit \r\n\r\n3.又例如我输入\r\n```\r\n\t \tstd::istringstream in2(\"test test\");\r\n\t \tRead(in2 );\r\n```\r\noutput is 1 2 4 0 test 1 2 4 **1** test 1 2 4 3 1 2 4 0\r\n中间输出个1我很诧异，好像没改变什么值，怎么返回的状态就改变了呢？\r\n\r\n4.又例如我输入\r\n```\r\n\t\tstd::istringstream in(\"test test\");\r\n\t\tRead(in >> std::noskipws);\r\n```\r\noutput is 1 2 4 0 test 1 2 4 2 1 2 4 0\r\n我又疑惑了： a.为何只输出一个test呢？加了noskipws，我觉得应该是直接将test test当作一个单词一次性输出的。 b.为什么这里返回的状态又变成2了？\r\n\r\n上面的1 2 4我认为的意思是eofbit   ailbit   badbit在计算机中是连续排列的。\r\n111 高位1代表badbit，低位1代表eofbit，所以在取它们值的时候，自动忽略其他位的值，所以取badbit就相当于100（4），eofbit相当于001（1)\r\n\r\n以上\r\n\r\n(最近忙于老师布置的作业，一个星期没看C++primer了，感觉拉下好多啊)\r\n顺便@Mooophy  提高一下答题效率，再此先行谢过2位！"},"comment":{"url":"https://api.github.com/repos/Ocxs/Cpp-Primer/issues/comments/85027349","html_url":"https://github.com/Ocxs/Cpp-Primer/issues/11#issuecomment-85027349","issue_url":"https://api.github.com/repos/Ocxs/Cpp-Primer/issues/11","id":85027349,"user":{"login":"pezy","id":1147451,"avatar_url":"https://avatars.githubusercontent.com/u/1147451?v=3","gravatar_id":"","url":"https://api.github.com/users/pezy","html_url":"https://github.com/pezy","followers_url":"https://api.github.com/users/pezy/followers","following_url":"https://api.github.com/users/pezy/following{/other_user}","gists_url":"https://api.github.com/users/pezy/gists{/gist_id}","starred_url":"https://api.github.com/users/pezy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pezy/subscriptions","organizations_url":"https://api.github.com/users/pezy/orgs","repos_url":"https://api.github.com/users/pezy/repos","events_url":"https://api.github.com/users/pezy/events{/privacy}","received_events_url":"https://api.github.com/users/pezy/received_events","type":"User","site_admin":false},"created_at":"2015-03-23T14:34:59Z","updated_at":"2015-03-23T14:34:59Z","body":"> 不过仍然有个小疑惑，我根据您说的，也自己在vs2013里看了，既然eofbit ailbit badbit是static const，不能改变值，那么我们所说的置位，实际上并没有置位，只是返回了一个状态，比如到达文件尾，eofbit值并没有改变，不过返回了一个1，告诉我们到达文件尾了，然后我们人为的给它一个定义，说这叫eofbit置位了，来方便人理解？ 是这个意思吗？\r\n\r\n看来你还没理解透。\r\n\r\n- 置位，置的是什么？是 [std::ios_base::iostate](http://www.cplusplus.com/reference/ios/ios_base/iostate/)\r\n\r\n- @Mooophy 说 `+`，实际有失偏颇。实际是 `|`(或) 的关系。如 `eofbit(1)` 和 `failbit(2)`两个状态同时发生的时候，实际是：\r\n```sh\r\n    0001\r\n|   0010\r\n----------\r\n    0011\r\n```\r\n即 `std::ios_base::iostate` 等于 3，这是你在 SO 上原问题最大的疑惑（看见你追问了。。。）\r\n\r\n- Dietmar Kühl 大爷，是为了给你演示每一个状态出现的场景，才列出了各种情形。其他的你理解了，但 `noskipws` 好像还有疑问。@Mooophy 说的够清楚，我只补充一个示例代码：\r\n\r\n```cpp\r\nchar a, b, c;\r\n\r\nstd::istringstream iss (\"  123\");\r\niss >> std::skipws >> a >> b >> c;\r\nstd::cout << a << b << c << '\\n';\r\n\r\niss.seekg(0);\r\niss >> std::noskipws >> a >> b >> c;\r\nstd::cout << a << b << c << '\\n';\r\n```\r\n\r\n输出结果：\r\n```cpp\r\n123\r\n  1\r\n```\r\n"}}