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.
Name | Definition |
---|---|
char_type | char,wchar_t |
traits_type | char_traits<char_type> |
int_type | char_traits<char_type>::int_type |
pos_type | char_traits<char_type>::pos_type |
off_type | char_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
Name | Description |
---|---|
|
The mode constants are defined in ios_base class. |
Methods
Name Description basic_streambuf* rdbuf() Returns pointer to the underlying raw file device object.
Exampleifstream 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().Exampleifstream file("/usr/share/dict/words");
bool bsuccess = file.is_open();
- void open
(const char* filename,
ios_base::openmode mode = ios_base::in)- void open
(const string& filename,
ios_base::openmode mode = ios_base::in)
- 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.- Calls the first version with open(filename.c_str(),mode)
The mode constants are defined in ios_base class.Exampleifstream 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.Exampleifstream file; file.open("/usr/share/dict/words");
file.close();
No comments:
Post a Comment