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.
where CharT can be char or wchar_t.
Syntax
template<class CharT, class Traits = char_traits<CharT>> class basic_filebuf:public basic_streambuf<CharT,Traits>
member typesFollowing 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 |
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.
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.
Name | Description |
---|---|
in | Open file for reading. |
out | Open file for writing. |
ate | Set the stream's position indicator to the end of the stream on opening. |
app | Set the stream's position indicator to the end of the stream before each output operation. |
trunc | Any current content is discarded, assuming a length of zero on opening. |
binary | Consider stream as binary rather than text. |
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 ifstream, ofstream and fstream classes.
Name Description bool is_open() Checks if the file stream has an associated file.Returns true if it's open. Otherwise, false.
- basic_filebuf* open
(const char* filename,
ios_base::openmode mode)- basic_filebuf* open
(const string& filename,
ios_base::openmode mode)
- Opens and associates the file with name filename with the file stream.
If the call fails, returns nullptr, otherwise returns this.- 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. Examplefilebuf 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();
No comments:
Post a Comment