std::basic_filebuf::open
性病:基本[医]文件::打开
std::basic_filebuf | (1) | |
---|---|---|
std::basic_filebuf<CharT, Traits>* open( const std::string& str, std::ios_base::openmode mode ) | (2) | (since C++11) |
std::basic_filebuf<CharT, Traits>* open( const std::filesystem::path& p, std::ios_base::openmode mode ) | (3) | (since C++17) |
std::basic_filebuf<CharT, Traits>* open( const std::filesystem::path::value_type* s, std::ios_base::openmode mode ) | (4) | (since C++17) |
以给定的名称%28打开文件s
,,,p.c_str()
%28自C++17%29或str.c_str()
,取决于过载%29。
Overload (4) is only provided if std::filesystem::path::value_type is not char. | (since C++17) |
---|
打开该文件时,就好像通过调用std::fopen
(s, modestring)
,在哪里modestring
决定如下:
modestring | openmode & ~ate | Action if file already exists | Action if file does not exist |
---|---|---|---|
"r" | in | Read from start | Failure to open |
"w" | out, out|trunc | Destroy contents | Create new |
"a" | app, out|app | Append to file | Create new |
"r+" | out|in | Read from start | Error |
"w+" | out|in|trunc | Destroy contents | Create new |
"a+" | out|in|app, in|app | Write to end | Create new |
"rb" | binary|in | Read from start | Failure to open |
"wb" | binary|out, binary|out|trunc | Destroy contents | Create new |
"ab" | binary|app, binary|out|app | Write to end | Create new |
"r+b" | binary|out|in | Read from start | Error |
"w+b" | binary|out|in|trunc | Destroy contents | Create new |
"a+b" | binary|out|in|app, binary|in|app | Write to end | Create new |
如果openmode
不是列出的模式之一,open()
失败了。
如果打开的操作成功,并且openmode &
std::ios_base::ate
!=
0
%28ate
位设置为%29,将文件位置重新定位到文件末尾,就像通过调用std::fseek
(file, 0,
SEEK_END
)
如果重新定位失败,调用close()
并返回指示失败的空指针。
如果关联文件已经打开,则立即返回一个空指针。
参数
s, str, p | - | the file name to open |
---|---|---|
openmode | - | the file opening mode, a binary OR of the std::ios_base modes |
返回值
this
如果成功,则为失败的空指针。
注记
open()
通常通过构造函数或open()
成员函数std::basic_fstream
...
例
另见
is_open | checks if the associated file is open (public member function) |
---|---|
close | flushes the put area buffer and closes the associated file (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。