public member function
<fstream>

std::fstream::fstream

默认 (1)
fstream();
initialization (2)
explicit fstream (const char* filename,                  ios_base::openmode mode = ios_base::in | ios_base::out);
默认 (1)
fstream();
initialization (2)
explicit fstream (const char* filename,                  ios_base::openmode mode = ios_base::in | ios_base::out);explicit fstream (const string& filename,                  ios_base::openmode mode = ios_base::in | ios_base::out);
copy (3)
fstream (const fstream&) = delete;
move (4)
fstream (fstream&& x);
Construct object and optionally open file
Constructs an fstream object

(1) 默认构造函数
Constructs an fstream object that is not associated with any file.
Internally, its iostream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer).
(2) initialization constructor
Constructs a fstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
Internally, its iostream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). Then, filebuf::open is called with filename and mode as arguments.
如果文件无法打开,则设置流的failbit标志。
(3) 复制构造函数 (已删除)
已删除 (无复制构造函数)。
(4) move constructor
获取 x 的内容。
First, the function move-constructs both its base iostream class from x and a filebuf object from x's internal filebuf object, and then associates them by calling member set_rdbuf.
x 处于未指定但有效的状态。
The internal filebuf object has at least the same duration as the fstream object.

参数

filename
代表要打开的文件名的字符串。
有关其格式和有效性的具体说明取决于库实现和运行环境。
mode
描述文件请求的输入/输出模式的标志。
这是一个位掩码成员类型 openmode 的对象,由以下成员常量的组合构成:
成员常量代表access
in输入用于读取的文件:内部流缓冲区 支持输入操作。
下可用的类型输出用于写入的文件:内部流缓冲区 支持输出操作。
binarybinary操作以二进制模式执行,而非文本模式。
ate在末尾输出位置从文件末尾开始。
appappend (追加)所有输出操作都在文件末尾进行,追加到其现有内容。
trunctruncate (截断)文件打开前存在的任何内容都将被丢弃。
这些标志可以通过按位或运算符(|)组合。

如果模式同时设置了 truncapp,则打开操作失败。如果仅设置了其中一个但未设置 out,或者同时设置了 appin,则也失败。
如果模式同时设置了 truncapp,则打开操作失败。如果设置了 trunc 但未设置 out,操作也会失败。
x
An fstream object, whose value is moved.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
// fstream constructor.
#include <fstream>      // std::fstream

int main () {

  std::fstream fs ("test.txt", std::fstream::in | std::fstream::out);

  // i/o operations here

  fs.close();

  return 0;
}

数据竞争

移动构造函数 (4) 会修改 x

异常安全

-

另见