2016-04-14 5 views
-1
次のコードで

(fstreamTest)がチェックするものは何ですか?特定のフラグをチェックしていますか?

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

if (testFile) 
cout << "This if statement is true"; 

Cがif(testfile)からtrueを返すためには何をチェックする++のでしょうか? testFile.goodbit()が本当ですか?

+1

http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool –

+1

http://www.cplusplus.com/reference:

これにより、のようなものをやってます/ ios/ios/operator_bool/ – ForceBru

答えて

2

前述のように定義... http://en.cppreference.com/w/cpp/io/basic_ios/operator_boolまたはhttp://www.cplusplus.com/reference/ios/ios/operator_bool/を検索できます。最後に、trueまたはfalseを返すfstreamクラスのメソッドを呼び出します。

法上のドキュメントは言う:

Returns true if the stream has no errors occurred and is ready 
of I/O operations. Specifically, returns !fail() 

ドキュメントの別のバージョンは言う:

Returns whether an error flag is set (either failbit or badbit). 

Notice that this function does not return the same as member good, 
but the opposite of member fail. 
3

(テストファイル)の場合からtrueを返すためにのためのC++のチェックを行いますか?ストリームにエラーがないかどうか

チェック:cppreferenceから



1) fail()がtrueを返す場合はnullポインタを返し、そうでない場合はnull以外のポインタを返します。この ポインタは暗黙的にboolに変換可能であり、ブール型 コンテキストで使用できます。 (C++11まで)

2)ストリームにエラーがなく、 I/O操作の準備ができている場合は、trueを返します。具体的には、!fail()を返します。

1)operator void*() const;までC++ 11)
2)explicit operator bool() const;以降C++ 11)

C++11ため)ストリームは、それが返すエラーがない場合true、それ以外の場合はfalseを返します。

while(stream >> value) { 
    ... 
} 
+0

C++ 11と2がそれを超えるまで1)が適用されることに言及する価値があります。 –

関連する問題