Share via


basic_ofstream 클래스

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

구문

template <class Elem, class Tr = char_traits<Elem>>
class basic_ofstream : public basic_ostream<Elem, Tr>

매개 변수

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

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

설명

wchar_t 파일에 쓰기를 특수화 basic_ofstream 할 때 파일이 텍스트 모드로 열리면 MBCS 시퀀스를 씁니다. 내부 표현에서는 wchar_t자의 버퍼를 사용하게 됩니다.

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

예시

다음 예제에서는 basic_ofstream 개체를 만들고 거기에 텍스트를 쓰는 방법을 보여 줍니다.

// basic_ofstream_class.cpp
// compile with: /EHsc
#include <fstream>

using namespace std;

int main(int argc, char **argv)
{
    ofstream ofs("ofstream.txt");
    if (!ofs.bad())
    {
        ofs << "Writing to a basic_ofstream object..." << endl;
        ofs.close();
    }
}

생성자

생성자 Description
basic_ofstream basic_ofstream 형식의 개체를 만듭니다.

멤버 함수

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

연산자

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

요구 사항

헤더<fstream>:

네임스페이스:std

basic_ofstream::basic_ofstream

basic_ofstream 형식의 개체를 만듭니다.

basic_ofstream();

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

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

basic_ofstream(
    basic_ofstream&& right);

매개 변수

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

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

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

right
basic_ofstream 개체를 초기화하는 데 사용되는 basic_ofstream 개체에 대한 rvalue 참조입니다.

설명

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

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

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

자세한 내용은 basic_ostream, opensetstate를 참조하세요.

예시

다음 예제에서는 basic_ofstream 개체를 만들고 거기에 텍스트를 쓰는 방법을 보여 줍니다.

// basic_ofstream_ctor.cpp
// compile with: /EHsc
#include <fstream>

using namespace std;

int main(int argc, char **argv)
{
    ofstream ofs("C:\\ofstream.txt");
    if (!ofs.bad())
    {
        ofs << "Writing to a basic_ofstream object..." << endl;
        ofs.close();
    }
}

basic_ofstream::close

파일을 닫습니다.

void close();

설명

멤버 함수는 .를 호출합니다 rdbuf->close. 자세한 내용은 rdbufclose를 참조하세요.

예시

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

basic_ofstream::is_open

파일이 열려 있는지 여부를 나타냅니다.

bool is_open() const;

Return Value

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

설명

멤버 함수는 rdbuf->is_open를 반환합니다. 자세한 내용은 rdbufis_open를 참조하세요.

예시

// basic_ofstream_is_open.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>

int main( )
{
   using namespace std;
   ifstream file;
   // Open and close with a basic_filebuf
   file.rdbuf( )->open( "basic_ofstream_is_open.txt", ios::in );
   file.close( );
   if (file.is_open())
      cout << "it's open" << endl;
   else
      cout << "it's closed" << endl;
}

basic_ofstream::open

파일을 엽니다.

void open(
    const char* _Filename,
    ios_base::openmode _Mode = ios_base::out,
    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::out,
    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::out). 해당 함수가 포인터를 NULL 반환하는 경우 함수는 .를 호출합니다 setstate(failbit).

자세한 내용은 rdbuf, opensetstate를 참조하세요.

예시

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

basic_ofstream::operator=

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

basic_ofstream& operator=(basic_ofstream&& right);

매개 변수

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

Return Value

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

설명

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

basic_ofstream::rdbuf

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

basic_filebuf<Elem, Tr> *rdbuf() const

Return Value

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

예시

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

basic_ofstream::swap

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

void swap(basic_ofstream& right);

매개 변수

right
다른 basic_ofstream 개체에 대한 lvalue 참조입니다.

설명

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

참고 항목

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