它用于设置错误状态标志。 标志的当前值被覆盖:所有位被状态中的那些位替换 如果状态为goodbit
(为零),则所有错误标志被清除。
在调用此函数时,如果没有流缓冲区与流相关联,则会自动设置badbit
标志(无论该段的值是否在参数状态中传递)。
声明
下面是ios::clear
函数的声明。
void clear (iostate state = goodbit)
示例
下面的例子中演示了ios::clear
函数的使用。
#include <iostream>
#include <fstream>
int main () {
char buffer [80]
std::fstream myfile
myfile.open ("test.txt",std::fstream::in)
myfile << "test"
if (myfile.fail()) {
std::cout << "Error writing to test.txt/n"
myfile.clear()
}
myfile.getline (buffer,80)
std::cout << buffer << " successfully read from file./n"
return 0
}