c++11 标准模板(STL)(std::basic_fstream)(二)

定义于头文件 <fstream>

|---------------------------------------------------------------------------------------------------------------------------------------|
| template< class CharT, class Traits = std::char_traits<CharT> > class basic_fstream : public std::basic_iostream<CharT, Traits> |

类模板 basic_fstream 实现基于文件的流上的高层输入/输出。它将 std::basic_iostream 的高层接口赋予基于文件的缓冲( std::basic_filebuf )。

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

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

|------------|--------------------------|
| 类型 | 定义 |
| fstream | basic_fstream<char> |
| wfstream | basic_fstream<wchar_t> |

成员函数

构造文件流

复制代码
std::basic_fstream<CharT,Traits>::basic_fstream

|-------------------------------------------------------------------------------------------------------------------------------------------|-----|-----------|
| basic_fstream(); | (1) | |
| explicit basic_fstream( const char* filename, std::ios_base::openmode mode = ios_base::in|ios_base::out ); | (2) | |
| explicit basic_fstream( const std::filesystem::path::value_type* filename, std::ios_base::openmode mode = ios_base::in|ios_base::out ); | (3) | (C++17 起) |
| explicit basic_fstream( const std::string& filename, std::ios_base::openmode mode = ios_base::in|ios_base::out ); | (4) | (C++11 起) |
| explicit basic_fstream( const std::filesystem::path& filename, std::ios_base::openmode mode = ios_base::in|ios_base::out ); | (5) | (C++17 起) |
| basic_fstream( basic_fstream&& other ); | (6) | (C++11 起) |
| basic_fstream( const basic_fstream& rhs) = delete; | (7) | (C++11 起) |

构造新的文件流。

  1. 默认构造函数:构造不关联到文件的流:默认构造 std::basic_filebuf 并构造拥有指向此默认构造的 std::basic_filebuf 成员的基类。

2-3) 首先,进行同默认构造函数的步骤,然后通过调用 rdbuf()->open(filename, mode)(该调用效果上的细节见 std::basic_filebuf::open )关联流与文件。若 open() 调用返回空指针,则设置 setstate(failbit) 。仅若 std::filesystem::path::value_type 非 char 才提供重载 (3) 。 (C++17 起)

4-5) 同 basic_fstream(filename.c_str(), mode) 。

  1. 移动构造函数:首先,从 other 移动构造基类(这不影响 rdbuf() 指针),然后移动构造 std::basic_filebuf 成员,再调用 this->set_rdbuf() 安装新的 basic_filebuf 为基类中的 rdbuf() 指针。

  2. 复制构造函数被删除:此类不可复制。

参数

|----------|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| filename | - | 要打开的文件名 |
| mode | - | 指定打开模式。它是位掩码类型,定义下列常量: |--------|-------------| | 常量 | 解释 | | app | 每次写入前寻位到流结尾 | | binary | 以二进制模式打开 | | in | 为读打开 | | out | 为写打开 | | trunc | 在打开时舍弃流的内容 | | ate | 打开后立即寻位到流结尾 | |
| other | - | 用作源的另一文件流 |

调用示例

复制代码
#include <fstream>
#include <utility>
#include <string>
#include <iostream>

int main()
{
    //1) 默认构造函数:构造不关联到文件的流
    std::fstream fstream1;
    std::cout << "fstream1 is: " << (fstream1.is_open() ? "true" : "false") << std::endl;

    //2-3) 首先,进行同默认构造函数的步骤,
    std::string strFileName2 = "test2.txt";
    std::fstream fstream2(strFileName2.c_str(), std::ios::out);
    std::cout << "fstream2 is: " << (fstream2.is_open() ? "true" : "false") << std::endl;

    //4-5) 同 basic_fstream(filename.c_str(), mode) 。
    std::string strFileName3 = "test3.txt";
    std::fstream fstream3(strFileName3, std::ios::out);
    std::cout << "fstream3 is: " << (fstream3.is_open() ? "true" : "false") << std::endl;

    //6) 移动构造函数:首先,从 other 移动构造基类(这不影响 rdbuf() 指针)
    std::fstream fstream4(std::move(strFileName3));
    std::cout << "fstream4 is: " << (fstream4.is_open() ? "true" : "false") << std::endl;

    //7) 复制构造函数被删除:此类不可复制。
    return 0;
}

输出

析构函数

|--------------------------------|
| 析构 basic_fstream 和关联的缓冲区,并关闭文件 |

basic_fstream默认生成的析构函数将通过调用basic_filebuf的析构,从而间接调用close方法。即,会在析构中自动关闭文件。

相关推荐
wuyk5559 小时前
98.C语言易混难点:字符数组与字符串指针的底层差异
c语言·开发语言·c++·stm32·嵌入式硬件·算法
峥无9 小时前
从0到1手撕红黑树:封装实现 my_map 与 my_set(SGI-STL 源码级深度解析)
开发语言·c++·笔记·算法·stl
波特率1152009 小时前
C++新特性---属性说明符与标准属性
开发语言·c++
不会代码的小猴10 小时前
3. 控件学习1
开发语言·c++·笔记·qt·算法
烧酒同学14 小时前
【C++】记录size of std::vector的巧妙坑
开发语言·c++·图形渲染
@呱呱爱学习17 小时前
C++大成之路_ STL容器_ Vector
开发语言·c++
码匠许师傅17 小时前
【C++ 面试真题】19. 聊聊 C++ 的迭代器
开发语言·c++·面试
qeen8717 小时前
【数据结构】红黑树的算法原理解析与实现
开发语言·数据结构·c++·算法·红黑树
码匠许师傅18 小时前
【C++ 面试真题】20. 聊聊 C++ 的标准库常用算法
c++·算法·面试
白色冰激凌19 小时前
[SECS/GEM研究] (六)SML与数据序列化
c++·secs/gem