defined in the header file <fstream>
template< class CharT, |
std::basic_filebuf
is a std::basic_streambuf whose associated character sequence is a file. Both input sequences and output sequences are associated to the same file, and the associated file location is maintained for both operations.
The functions underflow() and overflow()/sync() do the actual I/O between the file and the get-put area of the buffer. CharT
When not char, most implementations store multibyte characters in a file and use the std::codecvt plane for wide/multibyte character conversion.
Two specializations are also defined for common character types:
type | definition |
filebuf |
basic_filebuf<char> |
wfilebuf |
basic_filebuf<wchar_t> |
public member function
Construct a basic_filebuf object
std::basic_filebuf<CharT,Traits>::basic_filebuf
basic_filebuf(); |
(1) | |
basic_filebuf( const std::basic_filebuf& rhs ) = delete; |
(2) | (since C++11) |
basic_filebuf( std::basic_filebuf&& rhs ); |
(3) | (since C++11) |
Constructs a new std::basic_filebuf
object.
1) Construct std::basic_filebuf
the object, and call the default constructor of std::basic_streambuf to initialize the base class. Created basic_filebuf
is not associated with a file, and is_open() returns false.
2) The copy constructor is deleted; std::basic_filebuf
non- copyconstructible (CopyConstructible).
3) Move-constructs an object by moving everything from another std::basic_filebuf
object , including the buffer, associated files, locale , open mode, is_open variable, and all other state . After moving, it is not associated with a file and rhs.is_open()==false. The pointers to members of the base class of and std::basic_streambuf of the base class of are guaranteed to point to different buffers unless they are null.rhs
std::basic_filebuf
rhs
rhs
*this
parameter
rhs | - | anotherbasic_filebuf |
Notice
Typically called by the constructor of std::basic_fstream.
Destroys the basic_filebuf object and closes the file if open
std::basic_filebuf<CharT,Traits>::~basic_filebuf
virtual ~basic_filebuf(); |
Calling close() closes the associated file and destroys basic_filebuf
all other members of the . If close() throws an exception, it is caught and not rethrown.
parameter
(none)
return value
(none)
Notice
Typically called by the std::basic_fstream destructor.
Assign basic_filebuf object
std::basic_filebuf<CharT,Traits>::operator=
std::basic_filebuf& operator=( std::basic_filebuf&& rhs ); |
(since C++11) | |
std::basic_filebuf& operator=( const std::basic_filebuf& rhs ) = delete; |
Assign another basic_filebuf
object.
1) First call close() to close the associated file, then move rhs
the contents of into *this
into : get and put buffers, locale, open mode, open flag, and any other state. After moving, rhs
not associated with a file and rhs.is_open() == false.
2) The copy assignment operator is deleted; CopyAssignable basic_filebuf
is not available .
parameter
rhs | - | another to be movedbasic_filebuf |
return value
*this
调用示例
#include <fstream>
#include <string>
#include <iostream>
int main()
{
std::ifstream fin("test.in"); // 只读
std::ofstream fout("test.out"); // 只写
std::string s;
getline(fin, s);
std::cout << s << '\n'; // 输出
*fin.rdbuf() = std::move(*fout.rdbuf());
getline(fin, s);
std::cout << s << '\n'; // 空行
std::cout << std::boolalpha << fout.is_open() << '\n'; // 打印 "false"
return 0;
}
output