描述物件,該物件會控制從 類別
basic_filebuf<Elem, Tr>
的資料流程緩衝區擷取專案和編碼物件,其類型
Elem
為 的元素,其字元特性是由 類別
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 其中一個列舉。
_Prot
開啟保護的預設檔案,相當於 shflag中的 _fsopen_wfsopen 參數。
第一個建構函式會呼叫 basic_istream(sb) 來初始化基類,其中 sb 是 類別 basic_filebuf<Elem, Tr> 的預存物件。 它也會藉由呼叫 basic_filebuf<Elem, Tr> 來初始化 sb 。
第二個和第三個建構函式會呼叫 basic_istream(sb) 來初始化基類。 它也會藉由呼叫 來初始化 sb ,然後 sb.open(_Filename, _Mode | ios_base::in) 。 basic_filebuf<Elem, Tr> 如果後者函式傳回 Null 指標,建構函式會呼叫 setstate(failbit) 。
第四個建構函式會使用 視為參考的內容 rightrvalue ,初始化 物件。
如需詳細資訊,請參閱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 其中一個列舉。
_Prot
開啟保護的預設檔案,相當於 shflag中的 _fsopen_wfsopen 參數。
成員函式會呼叫 rdbuf->open(_Filename, _Mode | ios_base::in) 。 如需詳細資訊,請參閱 rdbuf 和 basic_filebuf::open。 如果 open 失敗,函式會呼叫 setstate(failbit) ,這可能會擲回 ios_base::failure 例外狀況。 如需詳細資訊,請參閱setstate。
如需使用 open 的範例,請參閱 basic_filebuf::open 。
basic_ifstream::operator=
指派此資料流物件的內容。 這是牽涉 rvalue 到不會留下複本的移動指派。
basic_ifstream& operator=(basic_ifstream&& right);
right
basic_ifstream 物件的 rvalue 參考。
傳回 *this。
成員運算子會使用 視為參考的內容來取代 物件的內容 rightrvalue 。 如需詳細資訊,請參閱Lvalues 和 Rvalues。
basic_ifstream::rdbuf
傳回預存資料流緩衝區的位址。
basic_filebuf<Elem, Tr> *rdbuf() const
表示預存資料流程緩衝區之 物件的指標 basic_filebuf 。
如需使用 rdbuf 的範例,請參閱 basic_filebuf::close 。
basic_ifstream::swap
交換兩個 basic_ifstream 物件的內容。
void swap(basic_ifstream& right);
right
對另一個資料流緩衝區的參考。
成員函式會將此物件的內容與 right 的內容交換。
C++ 標準程式庫中的執行緒安全性
iostream 程式設計
iostreams 約定