basic_ifstream 클래스

클래스basic_filebuf<Elem, Tr>의 스트림 버퍼에서 요소 및 인코딩된 개체의 추출을 제어하는 개체를 클래스에 의해 Tr문자 특성이 결정되는 형식Elem의 요소를 사용하여 설명합니다.

자세한 내용은 basic_filebuf를 참조하세요.

구문

template <class Elem, class Tr = char_traits<Elem>>
class basic_ifstream : public basic_istream<Elem, Tr>

매개 변수

Elem
파일 버퍼의 기본 요소입니다.

Tr
파일 버퍼의 기본 요소 특성(일반적으로)입니다 char_traits<Elem>.

설명

이 개체는 basic_filebuf<Elem, Tr> 클래스의 개체를 저장합니다.

예시

다음 예제에서는 파일에서 텍스트를 읽는 방법을 보여 줍니다.

// basic_ifstream_class.cpp
// compile with: /EHsc

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    ifstream ifs("basic_ifstream_class.txt");
    if (!ifs.bad())
    {
        // Dump the contents of the file to cout.
        cout << ifs.rdbuf();
        ifs.close();
    }
}

입력: basic_ifstream_class.txt

This is the contents of basic_ifstream_class.txt.

출력

This is the contents of basic_ifstream_class.txt.

생성자

생성자 Description
basic_ifstream basic_ifstream 개체의 새 인스턴스를 초기화합니다.

멤버 함수

멤버 함수 설명
close 파일을 닫습니다.
is_open 파일이 열려 있는지 확인합니다.
open 파일을 엽니다.
rdbuf 저장된 스트림 버퍼 주소를 반환합니다.
swap basic_ifstream의 콘텐츠를 제공된 basic_ifstream의 콘텐츠로 교환합니다.

연산자

연산자 설명
operator= 이 스트림 개체의 콘텐츠를 할당합니다. 복사본을 남겨 두지 않는 이동 할당 rvalue 입니다.

요구 사항

헤더<fstream>:

네임스페이스:std

basic_ifstream::basic_ifstream

basic_ifstream 형식의 개체를 생성합니다.

basic_ifstream();

explicit basic_ifstream(
    const char* _Filename,
    ios_base::openmode _Mode = ios_base::in,
    int _Prot = (int)ios_base::_Openprot);

explicit basic_ifstream(
    const wchar_t* _Filename,
    ios_base::openmode _Mode = ios_base::in,
    int _Prot = (int)ios_base::_Openprot);

basic_ifstream(basic_ifstream&& right);

매개 변수

_Filename
열어야 할 파일의 이름입니다.

_Mode
의 열거형 중 하나입니다 ios_base::openmode.

_Prot
의 매개 변수_wfsopen_fsopen와 동일한 기본 파일 여는 shflag 보호입니다.

설명

첫 번째 생성자는 클래스의 저장된 개체를 sb 호출basic_istream(sb)하여 기본 클래스basic_filebuf<Elem, Tr>를 초기화합니다. 또한 호출basic_filebuf<Elem, Tr>하여 초기화합니다sb.

두 번째 및 세 번째 생성자는 호출 basic_istream(sb)하여 기본 클래스를 초기화합니다. 또한 다음을 sb.open(_Filename, _Mode | ios_base::in)호출basic_filebuf<Elem, Tr>하여 초기화합니다sb. 후자의 함수가 null 포인터를 반환하면 생성자가 호출 setstate(failbit)합니다.

네 번째 생성자는 참조로 처리되는 내용으로 개체를 rvalue 초기화합니다right.

자세한 내용은 다음을 참조하세요.basic_istream, basic_filebuf, setstateopen.

예시

다음 예제에서는 파일에서 텍스트를 읽는 방법을 보여 줍니다. 파일을 만들려면 .에 대한 basic_ofstream::basic_ofstream예제를 참조하세요.

// basic_ifstream_ctor.cpp
// compile with: /EHsc

#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    ifstream ifs("basic_ifstream_ctor.txt");
    if (!ifs.bad())
    {
        // Dump the contents of the file to cout.
        cout << ifs.rdbuf();
        ifs.close();
    }
}

basic_ifstream::close

파일을 닫습니다.

void close();

설명

멤버 함수는 ->close를 호출합니다.rdbuf

예시

를 사용하는 close예제를 참조하세요basic_filebuf::close.

basic_ifstream::is_open

파일이 열려 있는지 확인합니다.

bool is_open() const;

Return Value

파일이 열려 있는 경우 true, 아닌 경우 false입니다.

설명

멤버 함수는 ->is_open를 반환합니다.rdbuf

예시

를 사용하는 is_open예제를 참조하세요basic_filebuf::is_open.

basic_ifstream::open

파일을 엽니다.

void open(
    const char* _Filename,
    ios_base::openmode _Mode = ios_base::in,
    int _Prot = (int)ios_base::_Openprot);

void open(
    const char* _Filename,
    ios_base::openmode _Mode);

void open(
    const wchar_t* _Filename,
    ios_base::openmode _Mode = ios_base::in,
    int _Prot = (int)ios_base::_Openprot);

void open(
    const wchar_t* _Filename,
    ios_base::openmode _Mode);

매개 변수

_Filename
열어야 할 파일의 이름입니다.

_Mode
의 열거형 중 하나입니다 ios_base::openmode.

_Prot
의 매개 변수_wfsopen_fsopen와 동일한 기본 파일 여는 shflag 보호입니다.

설명

멤버 함수는 .를 호출합니다 rdbuf->open(_Filename, _Mode | ios_base::in). 자세한 내용은 rdbufbasic_filebuf::open를 참조하세요. 열기가 실패하면 함수는 예외를 throw할 수 있는 호출을 호출 setstate(failbit)합니다 ios_base::failure . 자세한 내용은 setstate를 참조하세요.

예시

를 사용하는 open예제를 참조하세요basic_filebuf::open.

basic_ifstream::operator=

이 스트림 개체의 콘텐츠를 할당합니다. 복사본을 남겨 두지 않는 이동 할당 rvalue 입니다.

basic_ifstream& operator=(basic_ifstream&& right);

매개 변수

right
basic_ifstream 개체에 대한 rvalue 참조입니다.

Return Value

*this을(를) 반환합니다.

설명

멤버 연산자는 참조로 rvalue 처리되는 내용을 사용하여 개체의 right내용을 바꿉니다. 자세한 내용은 LvaluesRvalues를 참조하세요.

basic_ifstream::rdbuf

저장된 스트림 버퍼 주소를 반환합니다.

basic_filebuf<Elem, Tr> *rdbuf() const

Return Value

저장된 스트림 버퍼를 basic_filebuf 나타내는 개체에 대한 포인터입니다.

예시

를 사용하는 rdbuf예제를 참조하세요basic_filebuf::close.

basic_ifstream::swap

basic_ifstream 개체의 내용을 교환합니다.

void swap(basic_ifstream& right);

매개 변수

right
다른 스트림 버퍼에 대한 참조입니다.

설명

멤버 함수는 이 개체의 내용을 right의 내용으로 교환합니다.

참고 항목

C++ 표준 라이브러리의 스레드 보안
iostream 프로그래밍
iostreams 규칙