std::basic_fstream<CharT,Traits>::basic_fstream

Constructs new file stream.

# Declarations

basic_fstream();
explicit basic_fstream( const char* filename,
std::ios_base::openmode mode
= std::ios_base::in | std::ios_base::out );
explicit basic_fstream( const std::filesystem::path::value_type* filename,
std::ios_base::openmode mode
= std::ios_base::in | std::ios_base::out );

(since C++17)

explicit basic_fstream( const std::string& filename,
std::ios_base::openmode mode
= std::ios_base::in | std::ios_base::out );

(since C++11)

template< class FsPath >
explicit basic_fstream( const FsPath& filename,
std::ios_base::openmode mode
= std::ios_base::in | std::ios_base::out );

(since C++17)

basic_fstream( basic_fstream&& other );

(since C++11)

basic_fstream( const basic_fstream& rhs ) = delete;

(since C++11)

# Parameters

# Example

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

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 460C++98the default argument of mode in overload (2)was missing (it is present in the synopsis)added
LWG 3430C++17std::filesystem::path overload led to unwanted conversionsavoided by making it a template

# See also