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

定义于头文件 <fstream>

template<

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

> class basic_ifstream : public std::basic_istream<CharT, Traits>

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

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

 

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

类型定义
ifstreambasic_ifstream<char>
wifstreambasic_ifstream<wchar_t>

成员函数

移动文件流

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

basic_ifstream& operator=( basic_ifstream&& other );

(C++11 起)

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

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

参数

other-要移动的文件流。

返回值

*this

调用示例

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

int main()
{
    std::cout << "ifstream1: " << std::endl;
    std::ifstream ifstream1("test.txt", std::ios::in);
    std::cout << "ifstream1 is: " << (ifstream1 ? "true" : "false") << std::endl;
    if (ifstream1.is_open())
    {
        std::string strLine;
        while (std::getline(ifstream1, strLine))
        {
            std::cout << strLine << std::endl;
            break;
        }
    }
    std::cout << std::endl;


    std::ifstream ifstream2 = std::move(ifstream1);
    std::cout << "ifstream1 is: " << (ifstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ifstream2 is: " << (ifstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << std::endl;

    std::cout << "ifstream2: " << std::endl;
    if (ifstream2.is_open())
    {
        std::string strLine;
        while (std::getline(ifstream2, strLine))
        {
            std::cout << strLine << std::endl;
        }
    }

    return 0;
}
输出

交换二个文件流

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

void swap( basic_ifstream& other );

(C++11 起)

交换流与 other 的状态。

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

参数

other-要交换状态的流

返回值

(无)

调用示例

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

int main()
{
    std::ifstream ifstream1("test1.txt", std::ios::in);
    std::cout << "ifstream1: " << std::endl;
    if (ifstream1.is_open())
    {
        std::string strLine;
        uint8_t index = 0;
        while (std::getline(ifstream1, strLine))
        {
            std::cout << strLine << std::endl;
            if (index > 1)
            {
                break;
            }
            index++;
        }
    }
    std::cout << std::endl;

    std::ifstream ifstream2("test2.txt", std::ios::in);
    std::cout << "ifstream2: " << std::endl;
    if (ifstream2.is_open())
    {
        std::string strLine;
        uint8_t index = 0;
        while (std::getline(ifstream2, strLine))
        {
            std::cout << strLine << std::endl;
            if (index > 1)
            {
                break;
            }
            index++;
        }
    }
    std::cout << std::endl;

    ifstream2.swap(ifstream1);

    std::cout << "ifstream1: " << std::endl;
    if (ifstream1.is_open())
    {
        std::string strLine;
        while (std::getline(ifstream1, strLine))
        {
            std::cout << strLine << std::endl;
        }
    }

    std::cout << std::endl;

    std::cout << "ifstream2: " << std::endl;
    if (ifstream2.is_open())
    {
        std::string strLine;
        while (std::getline(ifstream2, strLine))
        {
            std::cout << strLine << std::endl;
        }
    }

    return 0;
}
输出

 

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

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

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

(C++11 起)

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

参数

(无)

返回值

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

调用示例

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

int main()
{
    std::ifstream ifstream1("test1.txt", std::ios::in);
    std::cout << "ifstream1: " << std::endl;
    if (ifstream1.is_open())
    {
        while (ifstream1)
        {
            //返回指向底层未处理文件设备对象的指针。
            std::cout << ifstream1.rdbuf() << std::endl;
        }
    }

    std::cout << std::endl;
    return 0;
}
输出

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值