std::basic_ifstream<CharT,Traits>::basic_ifstream
From cppreference.com
< cpp | io | basic ifstream
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::basic_ifstream
Member functions
basic_ifstream::basic_ifstream
(C++11)
(C++11)
(C++26)
File operations
Non-member functions
(C++11)
basic_ifstream();
(1)
explicit basic_ifstream( const char* filename,
= std::ios_base::in );
(2)
= std::ios_base::in );
explicit basic_ifstream( const std::filesystem::path::value_type* filename,
= std::ios_base::in );
(3)
(since C++17)
= std::ios_base::in );
explicit basic_ifstream( const std::string & filename,
= std::ios_base::in );
(4)
(since C++11)
= std::ios_base::in );
template< class FsPath >
(5)
(since C++17)
explicit basic_ifstream( const FsPath& filename,
std::ios_base::openmode mode
basic_ifstream( basic_ifstream&& other );
(6)
(since C++11)
basic_ifstream( const basic_ifstream& rhs ) = delete;
(7)
(since C++11)
Constructs new file stream.
1) Default constructor: constructs a stream that is not associated with a file: default-constructs the std::basic_filebuf and constructs the base with the pointer to this default-constructed std::basic_filebuf member.
2,3) First, performs the same steps as the default constructor, then associates the stream with a file by calling rdbuf()->open(filename, mode | std::ios_base::in ) (see std::basic_filebuf::open for the details on the effects of that call). If the
open()
call returns a null pointer, sets setstate(failbit). Overload (3) is only provided if std::filesystem::path::value_type is not char.(since C++17)4,5) Same as basic_ifstream(filename.c_str(), mode). (5) participates in overload resolution only if
FsPath
is std::filesystem::path .(since C++17) 6) Move constructor. First, move-constructs the base class from other (which does not affect the
rdbuf()
pointer), then move-constructs the std::basic_filebuf member, then calls this->set_rdbuf() to install the new basic_filebuf
as the rdbuf()
pointer in the base class.7) The copy-constructor is deleted: this class is not copyable.
Contents
[edit] Parameters
filename
-
the name of the file to be opened
mode
-
specifies stream open mode. Following constants and bit-wise OR between them may be used:
other
-
another file stream to use as source
Constant
Explanation
app
seek to the end of stream before each write
binary
open in binary mode
in
open for reading
out
open for writing
trunc
discard the contents of the stream when opening
ate
seek to the end of stream immediately after open
noreplace (C++23)
open in exclusive mode
[edit] Example
Run this code
#include <fstream> #include <string> #include <utility> int main() { std::ifstream f0; std::ifstream f1("test.bin", std::ios::binary); std::string name = "example.txt"; std::ifstream f2(name); std::ifstream f3(std::move(f1)); }
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3430 | C++17 | std::filesystem::path overload led to unwanted conversions | avoided by making it a template |
[edit] See also
opens a file and configures it as the associated character sequence
(public member function of
(public member function of
std::basic_filebuf<CharT,Traits>
) [edit]