Namespaces
Variants

std::basic_ofstream<CharT,Traits>:: basic_ofstream

From cppreference.net

basic_ofstream ( ) ;
(1)
explicit basic_ofstream ( const char * filename,

std:: ios_base :: openmode mode

= std:: ios_base :: out ) ;
(2)
explicit basic_ofstream ( const std :: filesystem :: path :: value_type * filename,

std:: ios_base :: openmode mode

= std:: ios_base :: out ) ;
(3) (C++17부터)
explicit basic_ofstream ( const std:: string & filename,

std:: ios_base :: openmode mode

= std:: ios_base :: out ) ;
(4) (C++11부터)
template < class FsPath >

explicit basic_ofstream ( const FsPath & filename,
std:: ios_base :: openmode mode

= std:: ios_base :: out ) ;
(5) (C++17부터)
basic_ofstream ( basic_ofstream && other ) ;
(6) (C++11부터)
basic_ofstream ( const basic_ofstream & rhs ) = delete ;
(7) (C++11부터)

새로운 파일 스트림을 생성합니다.

1) 기본 생성자: 파일과 연관되지 않은 스트림을 생성합니다: std::basic_filebuf 를 기본 생성하고, 기본 생성된 이 std::basic_filebuf 멤버에 대한 포인터로 베이스를 생성합니다.
2,3) 먼저 기본 생성자와 동일한 단계를 수행한 후, rdbuf ( ) - > open ( filename, mode | std:: ios_base :: out ) 호출을 통해 스트림을 파일과 연결합니다(해당 호출의 효과에 대한 자세한 내용은 std::basic_filebuf::open 참조). open() 호출이 null 포인터를 반환하면 setstate ( failbit ) 를 설정합니다. 오버로드 (3) std :: filesystem :: path :: value_type char 가 아닌 경우에만 제공됩니다. (C++17부터)
4,5) basic_ofstream ( filename. c_str ( ) , mode ) 와 동일합니다. (5) FsPath std::filesystem::path 인 경우에만 오버로드 해결에 참여합니다. (C++17부터) 기본 모드가 out 임에도 불구하고, 효과는 std::filebuf::open 에 설명된 out | trunc 의 효과와 동일합니다.
6) 이동 생성자. 먼저, 기본 클래스를 other 에서 이동 생성합니다( rdbuf() 포인터에 영향을 주지 않음). 그런 다음 std::basic_filebuf 멤버를 이동 생성하고, this - > set_rdbuf ( ) 를 호출하여 새로운 basic_filebuf 를 기본 클래스의 rdbuf() 포인터로 설정합니다.
7) 복사 생성자가 삭제됨: 이 클래스는 복사할 수 없습니다.

목차

매개변수

filename - 열릴 파일의 이름
mode - 스트림 열기 모드를 지정합니다. 다음 상수들과 이들 간의 비트 OR 연산을 사용할 수 있습니다:
Constant Explanation
app 각 쓰기 전에 스트림의 끝으로 이동
binary 바이너리 모드 로 열기
in 읽기용으로 열기
out 쓰기용으로 열기
trunc 열 때 스트림 내용 삭제
ate 열기 직후 스트림의 끝으로 이동
noreplace (C++23) 배타 모드로 열기
other - 소스로 사용할 다른 파일 스트림

예제

#include <fstream>
#include <string>
#include <utility>
int main()
{
    std::ofstream f0;
    std::ofstream f1("test.bin", std::ios::binary);
    std::string name = "example.txt";
    std::ofstream f2(name);
    std::ofstream f3(std::move(f1));
}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3430 C++17 std::filesystem::path 오버로드가 원치 않는 변환을 유발함 템플릿으로 만들어 회피

참고 항목

파일을 열고 스트림과 연결합니다
(public member function)
파일을 열고 연결된 문자 시퀀스로 구성합니다
(public member function of std::basic_filebuf<CharT,Traits> )
rdbuf 를 교체하되 오류 상태를 지우지 않습니다
(protected member function)
객체를 생성합니다
(public member function of std::basic_ostream<CharT,Traits> )