当前位置: 首页 > news >正文

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

定义于头文件 <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> 的实例。

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

类型定义
ofstreambasic_ofstream<char>
wofstreambasic_ofstream<wchar_t>

 

成员函数

构造文件流

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

basic_ofstream();

(1)

explicit basic_ofstream( const char* filename,
                std::ios_base::openmode mode = ios_base::out );

(2)

explicit basic_ofstream( const std::filesystem::path::value_type* filename,
                std::ios_base::openmode mode = ios_base::out );

(3)(C++17 起)

explicit basic_ofstream( const std::string& filename,
                std::ios_base::openmode mode = ios_base::out );

(4)(C++11 起)

explicit basic_ofstream( const std::filesystem::path& filename,
                std::ios_base::openmode mode = ios_base::out );

(5)(C++17 起)

basic_ofstream( basic_ofstream&& other );

(6)(C++11 起)

basic_ofstream( const basic_ofstream& rhs) = delete;

(7)(C++11 起)

 构造新的文件流。

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

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

4-5) 同 basic_ofstream(filename.c_str(), mode) 。注意,尽管默认模式是 out ,效果等同于描述于 std::filebuf::open 的 out|trunc 的效果。

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

7) 复制构造函数被删除:此类不可复制。

参数

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

调用示例

#include <fstream>
#include <utility>
#include <string>
#include <iostream>using namespace std;int main()
{//1) 默认构造函数:构造不关联到文件的流std::ofstream ofstream1;std::cout << "ofstream1 is: " << (ofstream1.is_open() ? "true" : "false") << std::endl;//2-3) 首先,进行同默认构造函数的步骤,std::string strFileName2 = "test2.txt";std::ofstream ofstream2(strFileName2.c_str(), std::ios::out);std::cout << "ofstream2 is: " << (ofstream2.is_open() ? "true" : "false") << std::endl;//4-5) 同 basic_ofstream(filename.c_str(), mode) 。std::string strFileName3 = "test3.txt";std::ofstream ofstream3(strFileName3, std::ios::out);std::cout << "ofstream3 is: " << (ofstream3.is_open() ? "true" : "false") << std::endl;//6) 移动构造函数:首先,从 other 移动构造基类(这不影响 rdbuf() 指针)std::ofstream ofstream4(std::move(strFileName3));std::cout << "ofstream4 is: " << (ofstream4.is_open() ? "true" : "false") << std::endl;//7) 复制构造函数被删除:此类不可复制。return 0;
}

输出

 

析构 basic_ofstream 和关联的缓冲区,并关闭文件

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

http://www.lryc.cn/news/108147.html

相关文章:

  • k8s概念-pv和pvc
  • python算法指南程序员经典,python算法教程pdf百度云
  • 微服务使用步骤
  • Ubuntu 23.04 作为系统盘的体验和使用感受
  • 百分点科技跻身中国智慧应急人工智能解决方案市场前三
  • vscode如何退出/切换 github 账号
  • maven发布到中央仓库
  • C#IEnumberable<>
  • Flink非对齐checkpoint原理(Flink Unaligned Checkpoint)
  • Linux crontab命令:循环执行定时任务(详解)
  • Linux系统jenkins+newman+postman持续集成环境搭建
  • flutter:Future、Stream、RxDart
  • Jenkins安装、配置、自动化构建前(nodejs)后端(maven)项目
  • 【网络基础进阶之路】设计网络划分的实战详解
  • 艺术二维码 API 申请及使用
  • JVM GC ROOT分析
  • 记一道有趣的sql题
  • C高级【day2】
  • 认识Webpack插件Plugin;CleanWebpackPlugin插件;HtmlWebpackPlugin;DefinePlugin;Mode模式
  • Redis 6.0的新特性:多线程、客户端缓存与安全
  • 【雕爷学编程】MicroPython动手做(37)——驱动LCD与图文显示3
  • 自然语言处理从入门到应用——LangChain:提示(Prompts)-[提示模板:序列化提示信息]
  • 【LinearAlgebra】Chapter 12 - Linear Algebra in Probability Statistics
  • webshell详解
  • 数据结构 | 搜索和排序——搜索
  • 【python】对象
  • k8s概念-污点与容忍
  • “从零开始学习Spring Boot:构建高效、可扩展的Java应用程序“
  • 通向架构师的道路之tomcat集群
  • 结构体,枚举,联合大小的计算规则