命名空间
变体
操作

std::basic_ifstream

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

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

> class basic_ifstream : public std::basic_istream<CharT, Traits>

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

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

cpp/io/ios basecpp/io/basic ioscpp/io/basic istreamstd-basic ifstream-inheritance.svg

继承图

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

定义于头文件 <fstream>
类型 定义
std::ifstream std::basic_ifstream<char>
std::wifstream std::basic_ifstream<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) 实现定义类型,它是可平凡复制半正则[编辑]

[编辑] 成员函数

构造文件流
(public member function) [编辑]
(析构函数)
[virtual] (隐式声明)
销毁 basic_ifstream 和关联的缓冲区,关闭文件
(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

成员函数

格式化输入
提取格式化数据
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
非格式化输入
提取字符
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
读取下一个字符而不提取它
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
取消提取字符
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
将字符放入输入流
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
提取字符直到找到给定字符
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
提取并丢弃字符直到找到给定字符
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
提取字符块
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
提取已有的字符块
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
返回上次未格式化输入操作提取的字符数
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
定位
返回输入位置指示符
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
设置输入位置指示符
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]
杂项
与底层存储设备同步
(std::basic_istream<CharT,Traits> 的公共成员函数) [编辑]

成员类

为输入操作准备流实现基本逻辑
(std::basic_istream<CharT,Traits> 的公共成员类) [编辑]

继承自 std::basic_ios

成员类型

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

成员函数

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

继承自 std::ios_base

成员函数

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

成员类型和常量

类型 解释
流打开模式类型

还定义了以下常量

常量 解释[编辑]
app 每次写入前定位到流末尾[编辑]
二进制 二进制模式打开[编辑]
in 为读取打开[编辑]
out 为写入打开[编辑]
trunc 打开时丢弃流内容[编辑]
ate 打开后立即定位到流末尾[编辑]
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) [编辑]
指定事件类型
(枚举) [编辑]
回调函数类型
(typedef) [编辑]

[编辑] 注释

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

[编辑] 示例

#include <fstream>
#include <iostream>
#include <string>
 
int main()
{
    std::string filename = "Test.b";
 
    // prepare a file to read
    double d = 3.14;
    std::ofstream(filename, std::ios::binary)
        .write(reinterpret_cast<char*>(&d), sizeof d) << 123 << "abc";
 
    // open file for reading
    std::ifstream istrm(filename, std::ios::binary);
    if (!istrm.is_open())
        std::cout << "failed to open " << filename << '\n';
    else
    {
        double d;
        istrm.read(reinterpret_cast<char*>(&d), sizeof d); // binary input
        int n;
        std::string s;
        if (istrm >> n >> s)                               // text input
            std::cout << "read back from file: " << d << ' ' << n << ' ' << s << '\n';
    }
}

输出

read back from file: 3.14 123 abc