cin.setstate()和cin.clear()有什么区别?

mr_moran 2006-10-06 10:37:33
如题:
cin.setstate()和cin.clear()都可以设置一个标记位,如goodbit。
那么,他们究竟有什么区别呢 ?请高手指教~!
...全文
344 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2006-10-07
  • 打赏
  • 举报
回复
有很多接口互相间就是功能有重叠的。
clear内部是调setstate来实现的,就是为了让使用者更方便。
就象stl的很多容器,s.push_back(x)和s.insert(x, s.end())也重复的。
mr_moran 2006-10-07
  • 打赏
  • 举报
回复
有点清楚了setstate和clear的不同,可是,
clear ( rdstate() | state ) = setstate(state)
他们是完全等价么 ? 那么我们还需要setstate做什么呢 ?迷糊中。。。
mr_moran 2006-10-07
  • 打赏
  • 举报
回复
HOHO,KNOW! 原来是如此而已。 感谢~!
jixingzhong 2006-10-07
  • 打赏
  • 举报
回复
ios:: setstate

void setstate ( iostate state );
Set control state.
Modifies the current control state value by adding (bitwise OR) the error state flags passed as paremeter to the current state.
Any error bit already set is not cleared. If you want to modify the state flags without keeping the current value use clear instead.
The effect of this function is as to call:
clear ( rdstate() | state );


Parameters.

state
An object of type ios_base::iostate that can take as value any combination of the following state flag member constants:

badbit (critical error in stream buffer)
eofbit (End-Of-File reached while extracting)
failbit (failure extracting from stream)
These are static member constants inherited from ios_base. You may combine more than one state flag bitmask using the bitwise | (or) operator.

Return Value.
none

Basic template member declaration ( basic_ios<charT,traits> ):
void setstate ( iostate state );




ios:: clear

void clear ( iostate state = goodbit );
Set control states.
Sets a new value for the control state ignoring the existing value.


Parameters.

state
An object of type ios_base::iostate that can take as value any combination of the following state flag member constants:

badbit (critical error in stream buffer)
eofbit (End-Of-File reached while extracting)
failbit (failure extracting from stream)
goodbit (no error condition, represents the absence of the above bits)
These are static member constants inherited from ios_base. You may combine more than one state flag bitmask using the bitwise | (or) operator.
If this parameter is not specified, goodbit is assumed so any error state is cleared.

Return Value.
none

Example.


// clearing errors
#include <iostream>
#include <fstream>
using namespace std;

int main () {
char buffer [80];
fstream myfile;

myfile.open ("test.txt",fstream::in);

myfile << "test";
if (myfile.fail())
{
cout << "Error writing to test.txt\n";
myfile.clear();
}

myfile.getline (buffer,80);
cout << buffer << " successfully read from file.\n";

return 0;
}

In this example myfile is open for input operations, but we commit an output operation on it, so failbit is set. The example calls then clear in order that further operations like getline can be successfully performed on myfile.

Basic template member declaration ( basic_ios<charT,traits> ):
void clear ( iostate state = goodbit );
jixingzhong 2006-10-07
  • 打赏
  • 举报
回复
看名字就知道了 ,
一个是清除标记, 回复至初始状态,
一个设置状态 ...
taodm 2006-10-06
  • 打赏
  • 举报
回复
优先用clear。setstatus接口复杂,功能更强,所以使用也麻烦,尽量不用。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧