描述一个对象,该对象可控制从
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
默认文件打开保护,等同于 _fsopen
、_wfsopen
中的 shflag
参数。
第一个构造函数通过调用 basic_istream(sb)
初始化基类,其中 sb
是 basic_filebuf<Elem, Tr>
类的存储对象。 它还可以通过调用 basic_filebuf<Elem, Tr>
来初始化 sb
。
通过调用 basic_istream(sb)
,第二个和第三个构造函数可初始化基类。 通过调用 basic_filebuf<Elem, Tr>
,然后 sb.open(_Filename, _Mode | ios_base::in)
,它还可以初始化 sb
。 如果后一个函数返回一个空指针,构造函数将调用 setstate(failbit)
。
第四个构造函数初始化具有 right
的内容的对象,将其视为 rvalue
引用。
有关详细信息,请参阅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
默认文件打开保护,等同于 _fsopen
、_wfsopen
中的 shflag
参数。
此成员函数调用 rdbuf->open(_Filename, _Mode | ios_base::in)
。 有关详细信息,请参阅 rdbuf
和 basic_filebuf::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
。
成员运算符使用 right
内容替换该对象的内容,被视为 rvalue
引用。 有关详细信息,请参阅 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
约定