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方法。即,会在析构中自动关闭文件。

相关推荐
柠石榴2 小时前
【练习】【类似于子集问题】力扣491. 非递减子序列/递增子序列
c++·算法·leetcode·回溯
Ronin-Lotus3 小时前
程序代码篇---C/C++中的变量存储位置
c语言·c++···静态区·文字常量区·变量存储位置
Abdullah al-Sa3 小时前
Docker教程(喂饭级!)
c++·人工智能·docker·容器
进击的_鹏3 小时前
【C++】list 链表的使用+模拟实现
开发语言·c++·链表
C语言扫地僧4 小时前
RPC 框架项目剖析
c++·网络协议·学习·rpc
水月梦镜花5 小时前
数据结构:基数排序(c++实现)
开发语言·数据结构·c++
qing_0406036 小时前
数据结构——二叉搜索树
数据结构·c++·二叉树·二叉搜索树
Ljw...6 小时前
DeepSeek+Kimi生成高质量PPT
数据库·c++·powerpoint·ppt·deepseek
敲上瘾6 小时前
基础dp——动态规划
java·数据结构·c++·python·算法·线性回归·动态规划
禁默7 小时前
C++之旅-C++11的深度剖析(1)
开发语言·c++