它用於檢查流的狀態是否良好。
下面是ios::good()
函式的宣告。
bool good() const;
true
,如果沒有設定流的狀態標誌。false
,如果設定了流的狀態標誌(badbit
,eofbit
或failbit
)。下面的例子解釋了ios::good()的使用。
#include <iostream>
#include <sstream>
void print_state (const std::ios& stream) {
std::cout << " good()=" << stream.good();
std::cout << " eof()=" << stream.eof();
std::cout << " fail()=" << stream.fail();
std::cout << " bad()=" << stream.bad();
}
int main () {
std::stringstream stream;
stream.clear (stream.goodbit);
std::cout << "goodbit:"; print_state(stream); std::cout << '/n';
stream.clear (stream.eofbit);
std::cout << " eofbit:"; print_state(stream); std::cout << '/n';
stream.clear (stream.failbit);
std::cout << "failbit:"; print_state(stream); std::cout << '/n';
stream.clear (stream.badbit);
std::cout << " badbit:"; print_state(stream); std::cout << '/n';
return 0;
}
編譯和執行上面的程式,將產生以下結果 -
goodbit: good()=1 eof()=0 fail()=0 bad()=0
eofbit: good()=0 eof()=1 fail()=0 bad()=0
failbit: good()=0 eof()=0 fail()=1 bad()=0
badbit: good()=0 eof()=0 fail()=1 bad()=1