std::basic_ofstream

std::basic_ofstream (파일 출력 스트림)

파일 기반 스트림에 대한 고수준 출력 연산을 구현하는 클래스 템플릿이에요. 파일 기반 스트림 버퍼(std::basic_filebuf)와 고수준 인터페이스(std::basic_ostream)를 연결해요.

출처: cppreference

본문

<fstream> 헤더에 정의돼 있어요.

template<
    class CharT,
    class Traits = std::char_traits<CharT>
> class basic_ofstream : public std::basic_ostream<CharT, Traits>;

클래스 템플릿 basic_ofstream은 파일 기반 스트림에 대한 고수준 출력 연산을 구현해요. 파일 기반 스트림 버퍼(std::basic_filebuf)와 std::basic_ostream의 고수준 인터페이스를 연결해요. 전형적인 구현은 std::basic_filebuf<CharT, Traits> 인스턴스 하나만 상속되지 않은 데이터 멤버로 보관해요.

공통 문자 타입 typedef

  • std::ofstreamstd::basic_ofstream<char>
  • std::wofstreamstd::basic_ofstream<wchar_t>

멤버 타입

  • char_type, traits_type, int_type, pos_type, off_type.

멤버 함수

  • (constructor) — ofstream을 생성해요(선택적으로 파일을 열어요).
  • open — 파일을 열어요.
  • is_open — 파일이 열려 있는지 확인해요.
  • close — 파일을 닫아요.
  • rdbuf — 기반 filebuf에 접근해요.
  • operator= — 이동 대입.

예제

#include <fstream>

int main()
{
    std::ofstream ofs("test.txt");
    ofs << "Hello, ofstream!\n";
    // 소멸 시 자동으로 파일 닫힘
}

더 알아보기 (Learn more)

cppreference