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

異常安全

-

另見