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

相关推荐
杨忆30 分钟前
构建自己的开发工作台MFC
数据库·c++·mfc
wadesir44 分钟前
C++非对称加密实战指南(从零开始掌握RSA加密算法)
开发语言·c++
七禾页丫1 小时前
面试记录12 软件(c++)工程师
c++·面试·职场和发展
酷酷的佳2 小时前
C语言--数组作为函数参数
c++
爱装代码的小瓶子2 小时前
【c++进阶】从C++98到C++11的奇妙旅程(故事科普版)
开发语言·c++
superman超哥3 小时前
仓颉类型别名的使用方法深度解析
c语言·开发语言·c++·python·仓颉
superman超哥3 小时前
仓颉GC调优参数深度解析
c语言·开发语言·c++·python·仓颉
誰能久伴不乏4 小时前
Linux `epoll` 学习笔记:从原理到正确写法(含 ET 经典坑总结)
linux·服务器·网络·c++·ubuntu
(❁´◡`❁)Jimmy(❁´◡`❁)4 小时前
【算法】 二分图理论知识和判断方法
c++·算法
im_AMBER4 小时前
Leetcode 85 【滑动窗口(不定长)】最多 K 个重复元素的最长子数组
c++·笔记·学习·算法·leetcode·哈希算法