命名空间
变体
操作

std::basic_fstream

来自 cppreference.cn
< cpp‎ | io
定义于头文件 <fstream>
template<

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

> class basic_fstream : public std::basic_iostream<CharT, Traits>

类模板 basic_fstream 实现了基于文件的流的高级输入/输出操作。它将基于文件的流缓冲区 (std::basic_filebuf) 与 (std::basic_iostream) 的高级接口连接起来。

std::basic_fstream 的典型实现仅包含一个非派生数据成员:std::basic_filebuf<CharT, Traits> 的实例。

cpp/io/ios basecpp/io/basic ioscpp/io/basic istreamcpp/io/basic ostreamcpp/io/basic iostreamstd-basic fstream-inheritance.svg

继承关系图

为常见的字符类型提供了几个 typedef

定义于头文件 <fstream>
类型 定义
std::fstream std::basic_fstream<char>
std::wfstream std::basic_fstream<wchar_t>

内容

[编辑] 成员类型

成员类型 定义
char_type CharT[编辑]
traits_type Traits; 如果 Traits::char_type 不是 CharT,则程序是非良构的。[编辑]
int_type Traits::int_type[编辑]
pos_type Traits::pos_type[编辑]
off_type Traits::off_type[编辑]
native_handle_type(C++26) 实现定义 类型,它是 TriviallyCopyablesemiregular[编辑]

[编辑] 成员函数

构造文件流
(public member function) [编辑]
(析构函数)
[virtual] (隐式声明)
析构 basic_fstream 和关联的缓冲区,关闭文件
(virtual public member function) [编辑]
(C++11)
移动文件流
(public member function) [编辑]
(C++11)
交换两个文件流
(public member function) [编辑]
返回底层原始文件设备对象
(public member function) [编辑]
返回底层实现定义的句柄
(public member function) [编辑]
文件操作
检查流是否有关联的文件
(public member function) [编辑]
打开文件并将其与流关联
(public member function) [编辑]
关闭关联的文件
(public member function) [编辑]

[编辑] 非成员函数

特化 std::swap 算法
(function template) [编辑]

继承自 std::basic_istream

成员函数

格式化输入
提取格式化数据
(public member function of std::basic_istream<CharT,Traits>) [编辑]
非格式化输入
提取字符
(public member function of std::basic_istream<CharT,Traits>) [编辑]
读取下一个字符但不提取它
(public member function of std::basic_istream<CharT,Traits>) [编辑]
取消提取一个字符
(public member function of std::basic_istream<CharT,Traits>) [编辑]
将字符放回输入流
(public member function of std::basic_istream<CharT,Traits>) [编辑]
提取字符直到找到给定字符
(public member function of std::basic_istream<CharT,Traits>) [编辑]
提取并丢弃字符直到找到给定字符
(public member function of std::basic_istream<CharT,Traits>) [编辑]
提取字符块
(public member function of std::basic_istream<CharT,Traits>) [编辑]
提取已有的字符块
(public member function of std::basic_istream<CharT,Traits>) [编辑]
返回上次非格式化输入操作提取的字符数
(public member function of std::basic_istream<CharT,Traits>) [编辑]
定位
返回输入位置指示器
(public member function of std::basic_istream<CharT,Traits>) [编辑]
设置输入位置指示器
(public member function of std::basic_istream<CharT,Traits>) [edit]
其他
与底层存储设备同步
(public member function of std::basic_istream<CharT,Traits>) [编辑]

成员类

实现为输入操作准备流的基本逻辑
(public member class of std::basic_istream<CharT,Traits>) [编辑]

继承自 std::basic_ostream

成员函数

格式化输出
插入格式化数据
(public member function of std::basic_ostream<CharT,Traits>) [编辑]
非格式化输出
插入一个字符
(public member function of std::basic_ostream<CharT,Traits>) [编辑]
插入字符块
(public member function of std::basic_ostream<CharT,Traits>) [编辑]
定位
返回输出位置指示器
(public member function of std::basic_ostream<CharT,Traits>) [编辑]
设置输出位置指示器
(public member function of std::basic_ostream<CharT,Traits>) [编辑]
其他
与底层存储设备同步
(public member function of std::basic_ostream<CharT,Traits>) [edit]

成员类

实现为输出操作准备流的基本逻辑
(public member class of std::basic_ostream<CharT,Traits>) [编辑]

继承自 std::basic_ios

成员类型

成员类型 定义
char_type CharT
traits_type Traits
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type

成员函数

状态函数
检查是否没有错误发生,即 I/O 操作可用
(public member function of std::basic_ios<CharT,Traits>) [编辑]
检查是否已到达文件末尾
(public member function of std::basic_ios<CharT,Traits>) [编辑]
检查是否发生错误
(public member function of std::basic_ios<CharT,Traits>) [编辑]
检查是否发生了不可恢复的错误
(public member function of std::basic_ios<CharT,Traits>) [编辑]
检查是否发生错误(fail() 的同义词)
(public member function of std::basic_ios<CharT,Traits>) [编辑]
检查是否没有错误发生(!fail() 的同义词)
(public member function of std::basic_ios<CharT,Traits>) [编辑]
返回状态标志
(public member function of std::basic_ios<CharT,Traits>) [编辑]
设置状态标志
(public member function of std::basic_ios<CharT,Traits>) [编辑]
修改状态标志
(public member function of std::basic_ios<CharT,Traits>) [编辑]
格式化
复制格式化信息
(public member function of std::basic_ios<CharT,Traits>) [编辑]
管理填充字符
(public member function of std::basic_ios<CharT,Traits>) [编辑]
其他
管理异常掩码
(public member function of std::basic_ios<CharT,Traits>) [编辑]
设置区域设置
(public member function of std::basic_ios<CharT,Traits>) [编辑]
管理关联的流缓冲区
(public member function of std::basic_ios<CharT,Traits>) [编辑]
管理绑定的流
(public member function of std::basic_ios<CharT,Traits>) [编辑]
窄化字符
(public member function of std::basic_ios<CharT,Traits>) [编辑]
宽化字符
(public member function of std::basic_ios<CharT,Traits>) [edit]

继承自 std::ios_base

成员函数

格式化
管理格式标志
(public member function of std::ios_base) [编辑]
设置特定格式标志
(public member function of std::ios_base) [编辑]
清除特定格式标志
(public member function of std::ios_base) [编辑]
管理浮点运算的十进制精度
(public member function of std::ios_base) [编辑]
管理字段宽度
(public member function of std::ios_base) [编辑]
区域设置
设置区域设置
(public member function of std::ios_base) [编辑]
返回当前区域设置
(public member function of std::ios_base) [编辑]
内部可扩展数组
[静态]
返回程序范围唯一的整数,可以安全地用作 pword()iword() 的索引
(public static member function of std::ios_base) [编辑]
必要时调整私有存储的大小,并访问给定索引处的 long 元素
(public member function of std::ios_base) [编辑]
必要时调整私有存储的大小,并访问给定索引处的 void* 元素
(public member function of std::ios_base) [编辑]
其他
注册事件回调函数
(public member function of std::ios_base) [编辑]
设置 C++ 和 C I/O 库是否可互操作
(public static member function of std::ios_base) [编辑]
成员类
流异常
(public member class of std::ios_base) [编辑]
初始化标准流对象
(public member class of std::ios_base) [编辑]

成员类型和常量

类型 解释
流打开模式类型

还定义了以下常量

常量 解释[编辑]
app 每次写入前定位到流的末尾[编辑]
binary 二进制模式打开[编辑]
in 打开以进行读取[编辑]
out 打开以进行写入[编辑]
截断 打开时丢弃流的内容[编辑]
定位到末尾 打开后立即定位到流的末尾[编辑]
noreplace (C++23) 以独占模式打开[编辑]

(typedef) [编辑]
格式化标志类型

还定义了以下常量

常量 说明[编辑]
dec 对整数 I/O 使用十进制基数:参见 std::dec[编辑]
oct 对整数 I/O 使用八进制基数:参见 std::oct[编辑]
hex 对整数 I/O 使用十六进制基数:参见 std::hex[编辑]
basefield dec | oct | hex. 用于掩码操作[编辑]
left 左对齐(向右添加填充字符):参见 std::left[编辑]
right 右对齐(向左添加填充字符):参见 std::right[编辑]
internal 内部对齐(向内部指定点添加填充字符):参见 std::internal[编辑]
adjustfield left | right | internal. 用于掩码操作[编辑]
scientific 使用科学计数法生成浮点类型,如果与 fixed 结合使用,则使用十六进制表示法:参见 std::scientific[编辑]
fixed 使用定点表示法生成浮点类型,如果与 scientific 结合使用,则使用十六进制表示法:参见 std::fixed[编辑]
floatfield scientific | fixed. 用于掩码操作[编辑]
boolalpha 以字母数字格式插入和提取 bool 类型:参见 std::boolalpha[编辑]
showbase 生成前缀以指示整数输出的数字基数,货币 I/O 中需要货币指示符:参见 std::showbase[编辑]
showpoint 无条件地为浮点数输出生成小数点字符:参见 std::showpoint[编辑]
showpos 为非负数输出生成 + 字符:参见 std::showpos[编辑]
skipws 跳过某些输入操作前的引导空白符:参见 std::skipws[编辑]
unitbuf 在每次输出操作后刷新输出:参见 std::unitbuf[编辑]
uppercase 在某些输出操作中,将某些小写字母替换为大写等效字母:参见 std::uppercase[编辑]

(typedef) [编辑]
流状态类型

还定义了以下常量

常量 说明[编辑]
goodbit 没有错误[编辑]
badbit 不可恢复的流错误[编辑]
failbit 输入/输出操作失败(格式化或提取错误)[编辑]
eofbit 关联的输入序列已到达文件结尾[编辑]

(typedef) [编辑]
定位方向类型

还定义了以下常量

常量 说明[编辑]
beg 流的开始处[编辑]
end 流的结尾处[编辑]
cur 流位置指示器的当前位置[编辑]

(typedef) [编辑]
指定事件类型
(enum) [编辑]
回调函数类型
(typedef) [编辑]

[编辑] 注释

特性测试 Std 特性
__cpp_lib_fstream_native_handle 202306L (C++26) 原生句柄支持

[编辑] 示例

#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    std::string filename{"test.bin"};
    std::fstream s{filename, s.binary | s.trunc | s.in | s.out};
 
    if (!s.is_open())
        std::cout << "failed to open " << filename << '\n';
    else
    {
        // write
        double d{3.14};
        s.write(reinterpret_cast<char*>(&d), sizeof d); // binary output
        s << 123 << "abc";                              // text output
 
        // for fstream, this moves the file position pointer (both put and get)
        s.seekp(0);
 
        // read
        d = 2.71828;
        s.read(reinterpret_cast<char*>(&d), sizeof d); // binary input
        int n;
        std::string str;
        if (s >> n >> str)                             // text input
            std::cout << "read back from file: " << d << ' ' << n << ' ' << str << '\n';
    }
}

输出

read back from file: 3.14 123 abc

[编辑] 参见

从 I/O 流中读取数据到字符串
(函数模板) [编辑]