在线文档教程
C++
输入/输出 | Input/output

std::basic_filebuf::open

性病:基本[医]文件::打开

std::basic_filebuf* open( const char* s, std::ios_base::openmode mode )(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决定如下:

modestringopenmode & ~ateAction if file already existsAction if file does not exist
"r"inRead from startFailure to open
"w"out, out|truncDestroy contentsCreate new
"a"app, out|appAppend to fileCreate new
"r+"out|inRead from startError
"w+"out|in|truncDestroy contentsCreate new
"a+"out|in|app, in|appWrite to endCreate new
"rb"binary|inRead from startFailure to open
"wb"binary|out, binary|out|truncDestroy contentsCreate new
"ab"binary|app, binary|out|appWrite to endCreate new
"r+b"binary|out|inRead from startError
"w+b"binary|out|in|truncDestroy contentsCreate new
"a+b"binary|out|in|app, binary|in|appWrite to endCreate 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_openchecks if the associated file is open (public member function)
closeflushes the put area buffer and closes the associated file (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/basic[医]文件/打开