Namespaces
Variants

std::basic_ios<CharT,Traits>:: fail

From cppreference.net
bool fail ( ) const ;

연관된 스트림에서 오류가 발생한 경우 true 를 반환합니다. 구체적으로, rdstate() 에서 badbit 또는 failbit 가 설정된 경우 true 를 반환합니다.

다음 조건들이 ios_base::iostate 를 설정하는 목록은 failbit 또는 badbit 을 참조하십시오.

목차

매개변수

(없음)

반환값

true 오류가 발생한 경우, false 그 외의 경우.

예제

#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
    std::ifstream file("test.txt");
    if (!file) // operator! is used here
    {  
        std::cout << "File opening failed\n";
        return EXIT_FAILURE;
    }
    // typical C++ I/O loop uses the return value of the I/O function
    // as the loop controlling condition, operator bool() is used here
    for (int n; file >> n;)
       std::cout << n << ' ';
    std::cout << '\n';
    if (file.bad())
        std::cout << "I/O error while reading\n";
    else if (file.eof())
        std::cout << "End of file reached successfully\n";
    else if (file.fail())
        std::cout << "Non-integer data encountered\n";
}

참고 항목

다음 표는 가능한 모든 basic_ios 접근자들( good() , fail() 등)의 값을 ios_base::iostate 플래그들의 모든 가능한 조합에 대해 보여줍니다:

ios_base::iostate 플래그 basic_ios 접근자
eofbit failbit badbit good() fail() bad() eof() operator bool operator!
false false false true false false false true false
거짓 거짓 거짓 거짓 거짓
거짓 거짓 거짓 거짓 거짓 거짓
거짓 거짓 거짓 거짓
거짓 거짓 거짓 거짓 거짓 거짓
거짓 거짓 거짓
거짓 거짓 거짓 거짓
거짓 거짓
파일 오류를 확인합니다
(함수)