c++11 标准模板(STL)(std::basic_ofstream)(三)

定义于头文件 <fstream>

template<

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

> class basic_ofstream : public std::basic_ostream<CharT, Traits>

类模板 basic_ifstream 实现文件流上的高层输入操作。它将 std::basic_istream 的高层接口赋予基于文件的流缓冲( std::basic_filebuf )。

std::basic_ifstream 的典型实现仅保有一个非导出数据成员: std::basic_filebuf<CharT, Traits> 的实例。

 亦对常用字符类型定义二个特化:

类型 定义
ofstream basic_ofstream<char>
wofstream basic_ofstream<wchar_t>

成员函数

移动文件流

std::basic_ofstream<CharT,Traits>::operator=

basic_ofstream& operator=( basic_ofstream&& other );

(C++11 起)

 移动赋值文件流 other*this ,等效地移动赋值 std::basic_ostream 基类和关联的 std::basic_filebuf 。

other 置为无关联文件。注意基类移动赋值交换 *this 与 other 间的所有流状态变量(除了 rdbuf )。

参数

other - 要移动的文件流。

返回值

*this

调用示例

#include <fstream>
#include <utility>
#include <string>
#include <iostream>

int main()
{
    std::ofstream ofstream1("test1.txt", std::ios::out);
    std::cout << "ofstream1 is: " << (ofstream1 ? "true" : "false") << std::endl;
    ofstream1 << "hello" << " ";
    std::cout << std::endl;

    //移动赋值文件流 other 给 *this ,等效地移动赋值 std::basic_ostream 基类和关联的 std::basic_filebuf 。
    std::ofstream ofstream2 = std::move(ofstream1);
    std::cout << "ofstream1 is: " << (ofstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ofstream2 is: " << (ofstream2.is_open() ? "true" : "false") << std::endl;
    ofstream2 << "word" << "!";
    std::cout << std::endl;
    ofstream1.close();
    ofstream2.close();

    std::ifstream ifstream1("test1.txt", std::ios::in);
    std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
    if (ifstream1.is_open())
    {
        std::cout << ifstream1.rdbuf() << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

交换二个文件流

std::basic_ofstream<CharT,Traits>::swap

void swap( basic_ofstream& other );

(C++11 起)

交换流与 other 的状态。

通过调用 basic_ostream<CharT, Traits>::swap(other) 和 rdbuf()->swap(other.rdbuf()) 进行。

参数

other - 要交换状态的流

返回值

(无)

调用示例

#include <fstream>
#include <utility>
#include <string>
#include <iostream>

int main()
{
    std::ofstream ofstream1("test1.txt", std::ios::out);
    std::cout << "ofstream1 is: " << (ofstream1 ? "true" : "false") << std::endl;
    std::ofstream ofstream2("test2.txt", std::ios::out);
    std::cout << "ofstream2 is: " << (ofstream2 ? "true" : "false") << std::endl;
    std::cout << std::endl;

    ofstream1 << "hello 1" << " ";
    ofstream2 << "hello 2" << " ";
    //交换流与 other 的状态。
    //通过调用 basic_ostream<CharT, Traits>::swap(other) 和 rdbuf()->swap(other.rdbuf()) 进行。
    std::cout << "ofstream1.swap(ofstream2) " << std::endl;
    ofstream1.swap(ofstream2);
    ofstream1 << "hello 1" << " ";
    ofstream2 << "hello 2" << " ";
    ofstream1.close();
    ofstream2.close();
    std::cout << std::endl;

    std::ifstream ifstream1("test1.txt", std::ios::in);
    std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
    if (ifstream1.is_open())
    {
        std::cout << ifstream1.rdbuf() << std::endl;
    }
    std::cout << std::endl;

    std::ifstream ifstream2("test2.txt", std::ios::in);
    std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
    if (ifstream2.is_open())
    {
        std::cout << ifstream2.rdbuf() << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

输出

返回底层未处理的文件设备对象

std::basic_ofstream<CharT,Traits>::rdbuf

std::basic_filebuf<CharT, Traits>* rdbuf() const;

(C++11 起)

返回指向底层未处理文件设备对象的指针。

参数

(无)

返回值

指向底层未处理文件设备对象的指针。

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/132008583