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 对象。
同时访问同一个文件流缓冲区对象可能导致数据争用。

异常安全

基本保证:如果抛出异常,*文件流缓冲区*处于有效状态。

另见