このブラウザーはサポートされなくなりました。
Microsoft Edge にアップグレードすると、最新の機能、セキュリティ更新プログラム、およびテクニカル サポートを利用できます。
Microsoft Edge をダウンロードする
Internet Explorer と Microsoft Edge の詳細情報
Elem
型の要素を含む
basic_filebuf<Elem, Tr>
クラスのストリーム バッファーからの要素とエンコードされたオブジェクトの抽出を制御するオブジェクトを記述します。この型の特性は、
Tr
クラスによって決定されます。
詳細については、
basic_filebuf
を参照してください。
template <class Elem, class Tr = char_traits<Elem>>
class basic_ifstream : public basic_istream<Elem, Tr>
パラメーター
ファイル バッファーの基本要素。
ファイル バッファーの基本要素の特徴 (通常は char_traits<Elem>)。
このオブジェクトは、クラス basic_filebuf<Elem, Tr> のオブジェクトを格納します。
次の例は、ファイルからテキストを読み取る方法を示しています。
// basic_ifstream_class.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
ifstream ifs("basic_ifstream_class.txt");
if (!ifs.bad())
// Dump the contents of the file to cout.
cout << ifs.rdbuf();
ifs.close();
This is the contents of basic_ifstream_class.txt.
This is the contents of basic_ifstream_class.txt.
コンストラクター
コンストラクター
const char* _Filename,
ios_base::openmode _Mode = ios_base::in,
int _Prot = (int)ios_base::_Openprot);
explicit basic_ifstream(
const wchar_t* _Filename,
ios_base::openmode _Mode = ios_base::in,
int _Prot = (int)ios_base::_Openprot);
basic_ifstream(basic_ifstream&& right);
パラメーター
_Filename
開くファイルの名前。
_Mode
ios_base::openmode の列挙体の 1 つ。
_Prot
_fsopen、_wfsopen の shflag パラメーターと同等の、ファイルを開く際の既定の保護。
最初のコンストラクターは、basic_istream(sb) を呼び出すことで基底クラスを初期化します。ここで、sb はクラス basic_filebuf<Elem, Tr> の格納されているオブジェクトです。 また、basic_filebuf<Elem, Tr> を呼び出すことで sb の初期化もします。
2 番目と 3 番目のコンストラクターは、basic_istream(sb) を呼び出すことで基底クラスを初期化します。 また、basic_filebuf<Elem, Tr> を呼び出し、続いて sb.open(_Filename, _Mode | ios_base::in) を呼び出すことで sb の初期化もします。 後者の関数が Null ポインターを返す場合、コンストラクターは setstate(failbit) を呼び出します。
4 番目のコンストラクターは、rvalue 参照として扱われる right のコンテンツでオブジェクトを初期化します。
詳細については、「basic_istream」、「basic_filebuf」、「setstate」、および「open」を参照してください。
次の例は、ファイルからテキストを読み取る方法を示しています。 ファイルを作成するには、次の例 basic_ofstream::basic_ofstreamを参照してください。
// basic_ifstream_ctor.cpp
// compile with: /EHsc
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
ifstream ifs("basic_ifstream_ctor.txt");
if (!ifs.bad())
// Dump the contents of the file to cout.
cout << ifs.rdbuf();
ifs.close();
basic_ifstream::close
ファイルを閉じます。
void close();
メンバー関数は、rdbuf->close を呼び出します。
close の使用例については、「basic_filebuf::close」を参照してください。
basic_ifstream::is_open
ファイルが開いているかどうかを判断します。
bool is_open() const;
ファイルが開いている場合は true、それ以外の場合は false。
メンバー関数は、rdbuf->is_open を返します。
is_open の使用例については、「basic_filebuf::is_open」を参照してください。
basic_ifstream::open
ファイルを開きます。
void open(
const char* _Filename,
ios_base::openmode _Mode = ios_base::in,
int _Prot = (int)ios_base::_Openprot);
void open(
const char* _Filename,
ios_base::openmode _Mode);
void open(
const wchar_t* _Filename,
ios_base::openmode _Mode = ios_base::in,
int _Prot = (int)ios_base::_Openprot);
void open(
const wchar_t* _Filename,
ios_base::openmode _Mode);
パラメーター
_Filename
開くファイルの名前。
_Mode
ios_base::openmode の列挙体の 1 つ。
_Prot
_fsopen、_wfsopen の shflag パラメーターと同等の、ファイルを開く際の既定の保護。
メンバー関数は、rdbuf->open(_Filename, _Mode | ios_base::in) を呼び出します。 詳細については、次のトピックを参照してください。 rdbuf および basic_filebuf::open オープンに失敗すると、関数は ios_base::failure 例外をスローできる setstate(failbit) を呼び出します。 詳細については、setstateを参照してください。
open の使用例については、「basic_filebuf::open」を参照してください。
basic_ifstream::operator=
このストリーム オブジェクトの内容を割り当てます。 これは、rvalue が関係する移動代入で、コピーを残しません。
basic_ifstream& operator=(basic_ifstream&& right);
パラメーター
right
basic_ifstream オブジェクトへの rvalue 参照。
*this を返します。
メンバー演算子により、rvalue 参照として扱われる right の内容を使用して、オブジェクトの内容が置き換えられます。 詳細については、次のトピックを参照してください。 Lvalues および Rvalues
basic_ifstream::rdbuf
格納されたストリーム バッファーのアドレスを返します。
basic_filebuf<Elem, Tr> *rdbuf() const
格納されているストリーム バッファーを表すオブジェクトへの basic_filebuf ポインター。
rdbuf の使用例については、「basic_filebuf::close」を参照してください。
basic_ifstream::swap
2 つの basic_ifstream オブジェクトの内容を交換します。
void swap(basic_ifstream& right);
パラメーター
right
別のストリーム バッファーへの参照。
このメンバー関数は、right の内容を、このオブジェクトの内容と交換します。
C++ 標準ライブラリ内のスレッド セーフ
iostream プログラミング
iostreams の規則
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback.
フィードバックの送信と表示