GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::basic_filebuf::open(3) C++ Standard Libary std::basic_filebuf::open(3)

std::basic_filebuf::open - std::basic_filebuf::open


std::basic_filebuf<CharT, Traits>* open( const char* s, (1)
std::ios_base::openmode mode )
std::basic_filebuf<CharT, Traits>* open( const std::string& str, (2) (since C++11)
std::ios_base::openmode mode )
std::basic_filebuf<CharT, Traits>* open( const
std::filesystem::path& p, (3) (since C++17)
std::ios_base::openmode mode )
std::basic_filebuf<CharT, Traits>* open( const
std::filesystem::path::value_type* s, (4) (since C++17)
std::ios_base::openmode mode )


Opens the file with the given name (s
, p.c_str()
(since C++17) or str.c_str(), depending on the overload). openmode values may be
written as, e.g., std::ios::out|std::ios::app.


Overload (4) is only provided if std::filesystem::path::value_type is (since C++17)
not char.


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 Action if file does
exists 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.


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


this on success, a null pointer on failure.


open() is typically called through the constructor or the open() member function of
std::basic_fstream.

// Run this code


#include <fstream>
#include <iostream>


int main() {
std::string filename = "Test.b";
std::filebuf fb;
// prepare a file to read
double d = 3.14;
if (!fb.open(filename, std::ios::binary|std::ios::out)) {
std::cout << "Open file " << filename << " for write failed\n";
return 1;
}
fb.sputn(reinterpret_cast<char*>(&d), sizeof d);
fb.close();


// open file for reading
double d2 = 0.0;
if (!fb.open(filename, std::ios::binary|std::ios::in)) {
std::cout << "Open file " << filename << " for read failed\n";
return 1;
}
auto got = fb.sgetn(reinterpret_cast<char*>(&d2), sizeof d2);
if (sizeof(d2) != got) {
std::cout << "Read of " << filename << " failed\n";
} else {
std::cout << "Read back from file: " << d2 << '\n';
}
}


Read back from file: 3.14


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)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.