Wednesday, December 18, 2024

basic_ofstream

Overview
basic_ofstream are designed to send  outputs from destinations such as file system.

Details
The class template basic_ofstream provides support for high level output operations on character streams based on file systems. The supported operations include formatted output (e.g. integer values or whitespace-separated characters and characters strings) and unformatted output (e.g. raw characters and character arrays). 

Syntax
template<class CharT, class Traits = char_traits<CharT>> 
class basic_ofstream: public basic_ostream<CharT,Traits> 
where CharT can be char or wchar_t. It's derived from basic_istream class.

member types
Following properties are defined.
NameDefinition
char_typechar,wchar_t
traits_typechar_traits<char_type>
int_typechar_traits<char_type>::int_type
pos_typechar_traits<char_type>::pos_type
off_typechar_traits<char_type>::off_type

The openmode flags indicate how the streams such as disk files or memory streams can be opened for IO operations such as read, write or append.

Constructor
NameDescription
  1. basic_ofstream
    (const char* filename,
    ios_base::openmode mode = ios_base::out)
  2. basic_ofstream
    (const string& filename,
    ios_base::openmode mode = ios_base::out)
  1. Constructs a basic_ofstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
    Internally, its basic_ostream base constructor is passed a pointer to a newly constructed basic_filebuf object.  Then, basic_filebuf::open(filename,mode)  is called.
    If the call fails, the stream's failbit flag is set.
  2. Calls the first version with basic_ifstream(filename.c_str(),mode)
The mode constants are defined in ios_base class.

Example
ofstream file("test.txt");
bool bsuccess = (!file);

Methods
NameDescription
basic_streambuf* rdbuf()Returns pointer to the underlying raw file device object.

Example
ofstream file("test.txt");
auto sb = file.rdbuf();
bool is_open()
Checks if the file stream has an associated file.
Effectively calls rdbuf()->is_open().

Example
ofstream file("test.txt");
bool bsuccess = file.is_open();
  1. void open
    (
    const char* filename,
    ios_base::openmode mode = ios_base::out)
  2. void  open
    (
    const string& filename,
    ios_base::openmode mode = ios_base::out)
  1. Opens and associates the file with name filename with the file stream.
    Internally, calls rdbuf()->open(filename,mode).
    If the call fails, the stream's failbit flag is set. If the stream is already associated with a file (i.e., it is already open), calling this function fails.
  2. Calls the first version with open(filename.c_str(),mode)
The mode constants are defined in ios_base class.

Example
ifstream file;
file.open("test.txt");
void close()
Closes the file currently associated with the object, disassociating it from the stream.
Any pending output sequence is written to the file.
Effectively calls rdbuf()->close(). If an error occurs during operation, setstate(failbit) is called.

Example
ofstream file("test.txt");
file.close();

No comments:

Post a Comment