Showing posts with label I/O Stream. Show all posts
Showing posts with label I/O Stream. Show all posts

Thursday, April 3, 2025

basic_stringstream

Overview
basic_stringstream class is designed to read from and write to memory stream.

Details
The class template basic_stringstream provides support for high level input and output operations on memory 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_stringstream class is basically derived class of  basic_istringstream and basic_ostringstream. It has no additional functionality other than constructor. 
Syntax
template<class CharT, class Traits = char_traits<CharT>> 
class basic_stringstream: 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_stringstream
    (ios_base::openmode mode = ios_base::in)
  2. basic_stringstream
    (const basic_string& s,
    ios_base::openmode mode = ios_base::in)
  3. basic_stringstream
    (basic_stringstream&& buf)
  1. Constructs an empty basic_istringstream object.
  2. Constructs basic_istringstream object with the content from s.
  3. move constructor
The mode constants are defined in ios_base class.

Example
istringstream s, s2 {"hello, world!"};

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

Example
istringstream s {"hello, world!"};
auto sb = s.rdbuf();
  1. void str
    (
    const basic_string& s) 
  2. basic_string str()
  1. Sets the current contents of the stream to s
  2. Returns the current contents of the stream as string.
Example
istringstream ss,ss2;
ss.str("hello, world!");
ss2.str(ss.str());


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();



Saturday, January 18, 2025

Class Organization

Overview
The classes can be grouped into following categories - Primitive base, Intermediate base , disk based  streams and String based streams.

Details
Each of these group  of the classes can be further grouped into two more groups - Input/Output preparation and serialization.

Primitive base classes 
Preparation
These classes serve as base classes for intermediate base classes.
NameAliasParentDescription
ios_base
 Base class for the IO stream classes
basic_ios<char,char_traits<char>> ios ios_baseBase classes for input and output  stream classes for character type char.
basic_ios<wchar_t,char_traits<wchar_t>> wios ios_baseBase classes for input and output  stream classes for character type wchar_t.

Serialization
These base classes support read / write stream operations.
NameAliasParentDescription
basic_streambuf<char,char_traits<char>> streambuf Base classes for Serialization/Deserialization of streams for character type char.
basic_streambuf<wchar_t,char_traits<wchar_t>>wstreambuf Base classes for Serialization/Deserialization of streams for character type wchar_t.

Intermediate base classes
Preparation
These classes serve as base classes for file and string based streams. 
NameAliasParentDescription
basic_istream<char,char_traits<char>>istream iosBase classes for input stream classes for character type char.
basic_istream<wchar_t,char_traits<wchar_t>>wistream    wiosBase classes for input stream classes for character type wchar_t.
basic_ostream<char,char_traits<char>>ostream iosBase classes for output stream classes for character type char.
basic_ostream<wchar_t,char_traits<wchar_t>>wostream wiosBase classes for output stream classes for character type wchar_t.
basic_iostream<char,char_traits<char>> iostream
istream,
ostream
 Base classes for input and output stream classes for character type char.
basic_iostream<wchar_t,char_traits<wchar_t>> wiostream
wistream,
wostream
 Base classes for input and output stream classes for character type wchar_t.

Disk based streams
Preparation
These classes support file based stream operation.
NameAliasParentDescription
basic_ifstream<char,char_traits<char>> ifstream istreamFile based input stream class for character type char.
basic_ifstream<wchar_t,char_traits<wchar_t>>wifstream wistreamFile based input stream class for character type wchar_t.
basic_ofstream<char,char_traits<char>> ofstream ostreamFile based output stream class for character type char.
basic_ofstream<wchar_t,char_traits<wchar_t>>wofstream wostreamFile based output stream class for character type wchar_t.
basic_fstream<char,char_traits<char>> fstreamiostreamFile based input and output stream class for character type char.
basic_fstream<wchar_t,char_traits<wchar_t>> wfstreamwiostreamFile based input and output stream class for character type char. wchar_t.

Serialization
These classes support file based stream operations.
NameAliasParentDescription
basic_filebuf<char,char_traits<char>> filebuf streambufImplements Serialization/Deserialization of file streams for character type char.
basic_filebuf<wchar_t,char_traits<wchar_t>>wfilebuf wstreambufImplements Serialization/Deserialization of file streams for character type wchar_t.

String based streams 
Preparation
These classes support string based stream operation.
NameAliasParentDescription
basic_ostringstream<char,char_traits<char>> istringstream istreamString based input stream class for character type char.
basic_ostringstream<wchar_t,char_traits<wchar_t>>wistringstream wistreamString based input stream class for character type wchar_t.
basic_ostringstream<char,char_traits<char>> ostringstream ostreamString based output stream class for character type char.
basic_ostringstream<wchar_t,char_traits<wchar_t>>wostringstream wostreamString based output stream class for character type wchar_t.
basic_stringstream<char,char_traits<char>> stringstreamiostreamString based input and output stream class for character type char.
basic_stringstream<wchar_t,char_traits<wchar_t>> wstringstreamwiostreamString  based input and output stream class for character type char. wchar_t.

Serialization
These classes support string based stream operation.
NameAliasParentDescription
basic_stringbuf<char,char_traits<char>> stringbuf streambufImplements Serialization/Deserialization of string streams for character type char.
basic_stringbuf<wchar_t,char_traits<wchar_t>>wstringbuf wstreambufImplements Serialization/Deserialization of string streams for character type wchar_t.

Wednesday, December 18, 2024

basic_stringbuf

Overview
basic_stringbuf is a template based class used for read from or/and write to strings in the memory.

Details
istringstream, ostringstream and stringstream  classes uses a basic_stringbuf to  read from or/and write to strings. It's derived from basic_streambuf class.

Syntax
template<class CharT, class Traits = char_traits<CharT>, class Alloc = allocator<CharT>> 
class basic_stringbuf:public basic_streambuf<CharT,Traits> 
where CharT can be char or wchar_t

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

openmode
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.  These are used in calls such as fopen(). These are bitmask type flags that can be ORed. The following describes different values.
NameDescription
inOpen file for reading.
outOpen file for writing.
ateSet the stream's position indicator to the end of the stream on opening.
appSet the stream's position indicator to the end of the stream before each output operation.
truncAny current content is discarded, assuming a length of zero on opening.
binaryConsider stream as binary rather than text.
Note it's possible to have multiple flags set. i.e., in | out set at the same time.

Note that in case of ate, writes can be on done anywhere in the stream. For example, begin or middle. In case of app, writes can be done only in the end.
Note that difference between text and binary files is that in a text file individual lines are demarcated by a newline character \n or \r\n. In case of binary files there are no such demarcation.


Constructor
NameDescription
  1. basic_istringbuf
    (ios_base::openmode mode = ios_base::in | ios_base::out)
  2. basic_istringbuf
    (const basic_string& s,
    ios_base::openmode mode = ios_base::in ios_base::out)
  1. Constructs an empty basic_stringbuf object.
  2. Constructs basic_stringbuf object with the content from s.
The mode constants are defined in ios_base class.

Example
stringbuf s, s2 {"hello, world!"};

Methods
These are the public functions available for the clients like istringstreamostringstream and stringstream classes.

NameDescription
  1. void str
    (
    const basic_string& s) 

  2. basic_string str()
  1. Sets the current contents of the stream to s
  2. Returns the current contents of the stream as string.
Example
stringbuf ss,ss2;
ss.str("hello, world!");
ss2.str(ss.str());


basic_ostringstream

Overview
basic_ostringstream are designed to send outputs to sources such as strings.

Details
The class template basic_ostringstream provides support for high level output operations on character streams based on strings. 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 Alloc = allocator<charT>> 
class basic_ostringstream: public basic_istream<CharT,Traits> 
where CharT can be char or wchar_t. It's derived from basic_ostream class.

member types
Following properties are defined.
NameDefinition
char_typechar,wchar_t
traits_typechar_traits<char_type>
allocator_typeAllocator<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_ostringstream
    (ios_base::openmode mode = ios_base::out)
  2. basic_ostringstream
    (const basic_string& s,
    ios_base::openmode mode = ios_base::out)
  1. Constructs an empty basic_istreamstream object.
  2. Constructs basic_istreamstream object with the content from s.
The mode constants are defined in ios_base class.

Example
ostringstream s, s2 {"hello, world!"};

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

Example
ostringstream s {"hello, world!"};
auto sb = s.rdbuf();
  1. void str
    (
    const basic_string& s) 
  2. basic_string str()
  1. Sets the current contents of the stream to s
  2. Returns the current contents of the stream as string.
Example
ostringstream ss, ss2;
ss.str("hello, world!");
ss2.str(ss.str());

basic_istringstream

Overview
basic_istringstream are designed to receive inputs from sources such as strings.

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

Syntax
template<class CharT, class Traits = char_traits<CharT>, class Alloc = allocator<charT>> 
class basic_istringstream: public basic_istream<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>
allocator_typeAllocator<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_istringstream
    (ios_base::openmode mode = ios_base::in)
  2. basic_istringstream
    (const basic_string& s,
    ios_base::openmode mode = ios_base::in)
  1. Constructs an empty basic_istringstream object.
  2. Constructs basic_istringstream object with the content from s.
The mode constants are defined in ios_base class.

Example
istringstream s, s2 {"hello, world!"};

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

Example
istringstream s {"hello, world!"};
auto sb = s.rdbuf();
  1. void str
    (
    const basic_string& s) 
  2. basic_string str()
  1. Sets the current contents of the stream to s
  2. Returns the current contents of the stream as string.
Example
istringstream ss,ss2;
ss.str("hello, world!");
ss2.str(ss.str());

basic_filebuf

Overview
basic_filebuf is a template based class used for read from or/and write to files in the file system.

Details
ifstream, ofstream and fstream classes uses a basic_filebuf to read from or/and write to files. It's derived from basic_streambuf class.

Syntax
template<class CharT, class Traits = char_traits<CharT>> 
class basic_filebuf:public basic_streambuf<CharT,Traits> 
where CharT can be char or wchar_t.

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

openmode
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. These are used in calls such as fopen(). These are bitmask type flags that can be ORed. The following describes different values.
NameDescription
inOpen file for reading.
outOpen file for writing.
ateSet the stream's position indicator to the end of the stream on opening.
appSet the stream's position indicator to the end of the stream before each output operation.
truncAny current content is discarded, assuming a length of zero on opening.
binaryConsider stream as binary rather than text.
Note it's possible to have multiple flags set. i.e., in | out set at the same time.
Note that in case of ate, writes can be on done anywhere in the stream. For example, begin or middle. In case of app, writes can be done only in the end.
Note that difference between text and binary files is that in a text file individual lines are demarcated by a newline character \n or \r\n. In case of binary files there are no such demarcation.

Methods
These are the public functions available for the clients like ifstreamofstream and fstream classes.
NameDescription
bool is_open()
Checks if the file stream has an associated file.
Returns true if it's open. Otherwise, false.
  1. basic_filebuf* open
    (
    const char* filename,
    ios_base::openmode mode)
  2. basic_filebuf* open
    (
    const string& filename,
    ios_base::openmode mode)
  1. Opens and associates the file with name filename with the file stream.
    If the call fails, returns nullptr, otherwise returns this.
  2. Calls the first version with open(filename.c_str(),mode)
The mode constants are defined in ios_base class. 
If the mode has both ios_base::trunc and ios_base::app set, the opening operation fails. It also fails if ios_base::trunc is set but ios_base::out is not.
void close()
Closes the file currently associated with the object, disassociating it from the stream.
Any pending output sequence is written to the file.
Example
filebuf file;
    
file.open("/usr/share/dict/words",ios_base::in);
bool bsuccess = file.is_open();
file.close();

file.open("test.txt",ios_base::out);
bsuccess = file.is_open();
file.close();

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();

Tuesday, December 17, 2024

basic_ifstream

Overview
basic_ifstream are designed to receive inputs from sources such as file system.

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

Syntax
template<class CharT, class Traits = char_traits<CharT>> 
class basic_ifstream: public basic_istream<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_ifstream
    (const char* filename, ios_base::openmode mode = ios_base::in)
  2. basic_ifstream
    (const string& filename, ios_base::openmode mode = ios_base::in)
  1. Constructs a basic_ifstream 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_ifstream(filename.c_str(),mode)
The mode constants are defined in ios_base class.

Example
ifstream file("/usr/share/dict/words");
bool bsuccess = (!file);

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();