public member function
<fstream>

std::basic_filebuf::open

basic_filebuf* open (const char* filename,  ios_base::openmode mode);
basic_filebuf* open (const char* filename,  ios_base::openmode mode);basic_filebuf* open (const string filename, ios_base::openmode mode);
開啟檔案
Opens the file identified by argument filename, associating its content with the file stream buffer object to perform input/output operations on it. The operations allowed and some operating details depend on parameter mode.

If the object is already associated with a file (i.e., it is already open), this function fails.

引數

filename
包含要開啟的檔案的名稱的字串。
mode
描述檔案請求的輸入/輸出模式的標誌。
This is an object of the bitmask type ios_base::openmode that consists of a combination of the following constants
代表access
ios_base::in輸入File open for reading, supporting input operations.
ios_base::out輸出File open for writing, supporting output operations.
ios_base::binarybinary操作以二進位制模式執行,而非文字模式。
ios_base::ate在末尾The put pointer (pptr) starts at the end of the controlled output sequence.
ios_base::appappend (追加)所有輸出操作都在檔案末尾進行,追加到其現有內容。
ios_base::trunctruncate (截斷)檔案開啟前存在的任何內容都將被丟棄。
這些標誌可以透過按位或運算子(|)組合。

If the mode has both ios_base::trunc and ios_base::app set, the opening operation fails. It also fails if either is set but ios_base::out is not, or if both ios_base::app and ios_base::in are set.
If the mode has both ios_base::trunc and ios_base::app set, the opening operation fails. It also fails if ios_base::trunc is set but ios_base::out is not.

返回值

The function returns this if successful.
In case of failure, the file is not open, and a null pointer is returned.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// filebuf::open()
#include <iostream>
#include <fstream>

int main () {
  std::ifstream is;
  std::filebuf * fb = is.rdbuf();

  fb->open ("test.txt",std::ios::out|std::ios::app);

  // >> appending operations here <<

  fb->close();

  return 0;
}

資料競爭

修改 basic_filebuf 物件。
同時訪問同一個檔案流緩衝區物件可能導致資料爭用。

異常安全

基本保證:如果丟擲異常,*檔案流緩衝區*處於有效狀態。

另見