std::basic_...sic_ifstream(3)
πΈ NAME πΈ
okay so bestie π you're on the manual page std::basic_...sic_ifstream(3) for the C++ Standard Library β and the thing it's documenting is std::basic_ifstream::basic_ifstream. Yes, it's the constructor page. The constructor. Very main character energy. β¨
std::basic_ifstream::basic_ifstream - std::basic_ifstream::basic_ifstream (the file-stream constructor, slay).
β¨ SYNOPSIS β¨
basic_ifstream(); // (1)
explicit basic_ifstream( const char* filename,
std::ios_base::openmode mode
= std::ios_base::in ); // (2)
explicit basic_ifstream( const std::filesystem::path::value_type*
filename,
std::ios_base::openmode mode
= std::ios_base::in ); // (3) (since C++17)
explicit basic_ifstream( const std::string& filename,
std::ios_base::openmode mode
= std::ios_base::in ); // (4) (since C++11)
template< class FsPath >
explicit basic_ifstream( const FsPath& filename,
std::ios_base::openmode mode
= std::ios_base::in ); // (5) (since C++17)
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_filebufand constructs the base with the pointer to this default-constructedstd::basic_filebufmember. - 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)(seestd::basic_filebuf::openfor the details on the effects of that call). If theopen()call returns a null pointer, setssetstate(failbit). Overload (3) is only provided ifstd::filesystem::path::value_typeis notchar. (since C++17) - 4,5) Same as
basic_ifstream(filename.c_str(), mode). (5) participates in overload resolution only ifFsPathisstd::filesystem::path. (since C++17) - 6) Move constructor. First, move-constructs the base class from
other(which does not affect therdbuf()pointer), then move-constructs thestd::basic_filebufmember, then callsthis->set_rdbuf()to install the newbasic_filebufas therdbuf()pointer in the base class. - 7) The copy-constructor is deleted: this class is not copyable. π
π PARAMETERS π
filenameβ the name of the file to be opened.modeβ specifies stream open mode. The following constants and bit-wise OR between them may be used:appβ seek to the end of stream before each writebinaryβ open in binary modeinβ open for readingoutβ open for writingtruncβ discard the contents of the stream when openingateβ seek to the end of stream immediately after opennoreplace(C++23) β open in exclusive mode
otherβ another file stream to use as source.
π EXAMPLE π
Run this code, bestie π
// 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));
}
π¦ DEFECT REPORTS π¦
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π
- DR:
LWG 3430 - Applied to: C++17
- Behavior as published:
std::filesystem::pathoverload led to unwanted conversions - Correct behavior: avoided by making it a template
π₯ SEE ALSO π₯
openβ opens a file and associates it with the stream (public member function)openβ opens a file and configures it as the associated character sequence (public member function ofstd::basic_filebuf<CharT,Traits>)set_rdbufβ replaces the rdbuf without clearing its error state (protected member function)constructorβ constructs the object (public member function ofstd::basic_istream<CharT,Traits>)
that's the whole page, bestie π source/version/date: http://cppreference.com, 2024.06.10, manual page std::basic_...sic_ifstream(3). periodt. π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms