Namespaces
Variants

std::basic_ifstream<CharT,Traits>:: basic_ifstream

From cppreference.net

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) (C++17以降)
explicit basic_ifstream ( const std:: string & filename,

std:: ios_base :: openmode mode

= std:: ios_base :: in ) ;
(4) (C++11以降)
template < class FsPath >

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

= std:: ios_base :: in ) ;
(5) (C++17以降)
basic_ifstream ( basic_ifstream && other ) ;
(6) (C++11以降)
basic_ifstream ( const basic_ifstream & rhs ) = delete ;
(7) (C++11以降)

新しいファイルストリームを構築します。

1) デフォルトコンストラクタ: ファイルに関連付けられていないストリームを構築する: std::basic_filebuf をデフォルト構築し、基底クラスをこのデフォルト構築された std::basic_filebuf メンバへのポインタで構築する。
2,3) まずデフォルトコンストラクタと同じ手順を実行し、その後 rdbuf ( ) - > open ( filename, mode | std:: ios_base :: in ) を呼び出してストリームをファイルに関連付けます(この呼び出しの効果の詳細については std::basic_filebuf::open を参照)。 open() の呼び出しがnullポインタを返した場合、 setstate ( failbit ) を設定します。 オーバーロード (3) std :: filesystem :: path :: value_type char でない場合にのみ提供されます。 (C++17以降)
4,5) 以下と同様 basic_ifstream ( filename. c_str ( ) , mode ) . (5) は、 FsPath std::filesystem::path である場合にのみオーバーロード解決に参加する。 (C++17以降)
6) ムーブコンストラクタ。まず基底クラスを other からムーブ構築し(これは rdbuf() ポインタに影響しない)、次に std::basic_filebuf メンバをムーブ構築し、その後 this - > set_rdbuf ( ) を呼び出して、新しい basic_filebuf を基底クラスの rdbuf() ポインタとして設定する。
7) コピーコンストラクタは削除されています:このクラスはコピーできません。

目次

パラメータ

filename - 開くファイルの名前
mode - ストリームのオープンモードを指定します。以下の定数とそれらのビット単位ORを使用できます:
定数 説明
app 各書き込み前にストリームの終端へシーク
binary バイナリモード でオープン
in 読み取り用にオープン
out 書き込み用にオープン
trunc オープン時にストリームの内容を破棄
ate オープン直後にストリームの終端へシーク
noreplace (C++23) 排他モードでオープン
other - ソースとして使用する別のファイルストリーム

#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));
}

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 3430 C++17 std::filesystem::path オーバーロードが望ましくない変換を引き起こした テンプレート化することで回避

関連項目

ファイルを開き、ストリームに関連付ける
(public member function)
ファイルを開き、関連付けられた文字シーケンスとして設定する
( std::basic_filebuf<CharT,Traits> のpublic member function)
rdbuf を置き換える(エラー状態はクリアしない)
(protected member function)
オブジェクトを構築する
( std::basic_istream<CharT,Traits> のpublic member function)