std::basic_filebuf<CharT,Traits>::open
std::basic_filebuf<CharT, Traits>* 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) |
Opens the file with the given name (s
, p.c_str()
(since C++17) or str.c_str()
, depending on the overload).
Overload (4) is only provided if | (since C++17) |
The file is opened as if by calling std::fopen
with the second argument (mode
) determined as follows:
mode | 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 |
If openmode
is not one of the modes listed, the open()
fails.
If the open operation succeeds and openmode & std::ios_base::ate != 0
(the ate
bit is set), repositions the file position to the end of file, as if by calling std::fseek(file, 0, SEEK_END)
, where file
is the pointer returned by calling fopen
. If the repositioning fails, calls close()
and returns a null pointer to indicate failure.
If the associated file was already open, returns a null pointer right away.
Parameters
s, str, p | - | the file name to open; s must point to a null-terminated string |
openmode | - | the file opening mode, a binary OR of the std::ios_base modes |
Return value
this
on success, a null pointer on failure.
Notes
open()
is typically called through the constructor or the open()
member function of std::basic_fstream
.
Example
See also
checks if the associated file is open (public member function) |
|
flushes the put area buffer and closes the associated file (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/io/basic_filebuf/open