Thursday, April 3, 2025

basic_fstream

Overview
basic_fstream class is designed to read from and write to streams such as  files.

Details
The class template basic_fstream provides support for high level input and output operations on file streams. The supported operations include formatted input and output (e.g. integer values or whitespace-separated characters and characters strings) and unformatted input and output(e.g. raw characters and character arrays). 
basic_fstream class is basically derived class of  basic_ifstream and basic_ofstream. It has no additional functionality other than constructor. 

Syntax
template<class CharT, class Traits = char_traits<CharT>> 
class basic_fstream: public basic_iostream<CharT,Traits>
where CharT can be char or wchar_t. It's derived from basic_ios 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_fstream
    (const char* filename,
    ios_base::openmode mode = ios_base::in|ios_base::out)
  2. basic_ifstream
    (const string& filename,
    ios_base::openmode mode = ios_base::in|ios_base::out)
  3. basic_fstream( basic_fstream&& other)
  1. Constructs a basic_fstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
    Internally, its basic_istream 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_fstream(filename.c_str(),mode)
  3. move constructor
The mode constants are defined in ios_base class.

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

Example
ifstream file("/usr/share/dict/words");
auto sb = file.rdbuf();
bool is_open()
Checks if the file stream has an associated file.
Effectively calls rdbuf()->is_open().

Example
ifstream file("/usr/share/dict/words");
bool bsuccess = file.is_open();
  1. void open
    (
    const char* filename,
    ios_base::openmode mode = ios_base::in)
  2. void  open
    (
    const string& filename,
    ios_base::openmode mode = ios_base::in)
  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("/usr/share/dict/words");
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
ifstream file;
file.open("/usr/share/dict/words");
file.close();



No comments:

Post a Comment