std::basic_fstream
定义于头文件
<fstream> |
||
template<
class CharT, |
||
类模板 basic_fstream
实现基于文件的流上的高层输入/输出。它将 std::basic_iostream 的高层接口赋予基于文件的缓冲( std::basic_filebuf )。
std::basic_fstream
的典型实现只保有一个非导出数据成员: std::basic_filebuf<CharT, Traits> 的实例。
继承图
亦定义二个对于常用字符类型的特化:
定义于头文件
<fstream> |
|
类型 | 定义 |
fstream |
basic_fstream<char> |
wfstream |
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 |
成员函数
构造文件流 (公开成员函数) |
|
(析构函数)
[虚] (隐式声明)
|
析构 basic_fstream 和关联的缓冲区,并关闭文件 (虚公开成员函数) |
(C++11)
|
移动文件流 (公开成员函数) |
(C++11)
|
交换二个文件流 (公开成员函数) |
返回底层未处理的文件设备对象 (公开成员函数) |
|
文件操作 |
|
检查流是否有关联文件 (公开成员函数) |
|
打开文件,并将它与流关联 (公开成员函数) |
|
关闭关联文件 (公开成员函数) |
非成员函数
(C++11)
|
特化 std::swap 算法 (函数模板) |
继承自 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_ostream
成员函数
有格式输出 |
|
插入带格式数据 ( std::basic_ostream<CharT,Traits> 的公开成员函数) |
|
无格式输出 |
|
插入字符 ( std::basic_ostream<CharT,Traits> 的公开成员函数) |
|
插入字符块 ( std::basic_ostream<CharT,Traits> 的公开成员函数) |
|
寻位 |
|
返回输出位置指示器 ( std::basic_ostream<CharT,Traits> 的公开成员函数) |
|
设置输出位置指示器 ( std::basic_ostream<CharT,Traits> 的公开成员函数) |
|
杂项 |
|
与底层存储设备同步 ( std::basic_ostream<CharT,Traits> 的公开成员函数) |
成员类
为输出操作实现流准备的基本逻辑 ( 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操作 ( 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> 的公开成员函数) |
|
(C++11 前)(C++11 起)
|
检查是否没有发生错误(!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的IO库是否可以互操作 ( std::ios_base 的公开静态成员函数) |
成员类 |
|
流异常 ( std::ios_base 的公开成员类) |
|
初始化标准流对象 ( std::ios_base 的公开成员类) |
成员类型和常量 |
|||||||||||||||||||||||||||||||||||||||
类型 | 解释 | ||||||||||||||||||||||||||||||||||||||
流打开模式类型
亦定义下列常量:
(typedef) |
|||||||||||||||||||||||||||||||||||||||
格式化标志类型
亦定义下列常量:
(typedef) |
|||||||||||||||||||||||||||||||||||||||
流状态类型
亦定义下列常量:
(typedef) |
|||||||||||||||||||||||||||||||||||||||
寻位方向类型
亦定义下列常量:
(typedef) |
|||||||||||||||||||||||||||||||||||||||
指定事件类型 (枚举) |
|||||||||||||||||||||||||||||||||||||||
回调函数类型 (typedef) |
参阅
从 I/O 流读取数据到字符串 (函数模板) |
示例
#include <iostream> #include <fstream> #include <string> int main() { std::string filename = "test.bin"; std::fstream s(filename, std::fstream::binary | std::fstream::trunc | std::fstream::in | std::fstream::out); if (!s.is_open()) { std::cout << "failed to open " << filename << '\n'; } else { // 写入 double d = 3.14; s.write(reinterpret_cast<char*>(&d), sizeof d); // 二进制输出 s << 123 << "abc"; // 文本输出 // 对于 fstream ,这会移动文件位置指针(放置与获取) s.seekp(0); // 读取 s.read(reinterpret_cast<char*>(&d), sizeof d); // 二进制输入 int n; std::string str; if (s >> n >> str) // 文本输入 std::cout << "read back from file: " << d << ' ' << n << ' ' << str << '\n'; } }
输出:
read back from file: 3.14 123 abc