c++11 standard template (STL) (std::basic_filebuf) (2)

defined in the header file <fstream>

template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_filebuf : public std::basic_streambuf<CharT, Traits>

std::basic_filebufis 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. CharTWhen 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_filebufobject.

1) Construct std::basic_filebufthe object, and call the default constructor of std::basic_streambuf to initialize the base class. Created basic_filebufis not associated with a file, and is_open() returns false.

2) The copy constructor is deleted; std::basic_filebufnon- copyconstructible (CopyConstructible).

3) Move-constructs an object by moving everything from another std::basic_filebufobject , 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.rhsstd::basic_filebufrhsrhs*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_filebufall 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_filebufobject.

1) First call close() to close the associated file, then move rhsthe contents of into *thisinto : get and put buffers, locale, open mode, open flag, and any other state. After moving, rhsnot associated with a file and rhs.is_open() == false.

2) The copy assignment operator is deleted; CopyAssignable basic_filebufis 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

 

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/131820102