fstreamw:C++标准程式库中的一个w:头文件,定义了C++标准中的文件流输入输出的几个基本模板类。

类模板 编辑

basic_filebuf 编辑

实现了原始(raw)文件设备。

basic_ifstream 编辑

实现了高层文件流输入操作。

basic_ofstream 编辑

实现了高层文件流输出操作。

basic_fstream 编辑

实现了高层文件流输入/输出。

类型定义 编辑

类型名 类型定义
filebuf basic_filebuf<char>
wfilebuf basic_filebuf<wchar_t>
ifstream basic_ifstream<char>
wifstream basic_ifstream<wchar_t>
ofstream basic_ofstream<char>
wofstream basic_ofstream<wchar_t>
fstream basic_fstream<char>
wfstream basic_fstream<wchar_t>

全局函数 编辑

函数 说明
std::swap(std::basic_filebuf) std::swap算法的特化
std::swap(std::basic_ifstream) std::swap算法的特化
std::swap(std::basic_ofstream) std::swap算法的特化
std::swap(std::basic_fstream) std::swap算法的特化

字符集转换 编辑

对于Unicode I/O流函数运行于文本模式,源或目的流被假定为多字节字符序列。因此Unicode流输入函数转化多字节字符到宽字符(如同调用了mbtowc函数);此Unicode流输出函数转化宽字符到多字节字符(如同调用了wctomb 函数)。对于Unicode I/O流函数运行于二进制模式,文件被假定为Unicode,没有回车换行符的转换,也没有再输入输出时的字符转换。[1]例如,使用_setmode( _fileno( stdin ), _O_BINARY );指示wcin运行于UNICODE文本文件上.

示例如下:

#include <locale> 
#include <codecvt>
#include <fstream>

int main()
{
	// UTF-8 data with BOM
	std::ofstream("text.txt") << u8"\ufeffz\u6c34\U0002A6A5";
	// read the UTF8 file, skipping the BOM
	std::wifstream fin("text.txt");
	fin.imbue(std::locale(fin.getloc(),new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));
	for (wchar_t c; fin.get(c); )
		std::cout << std::hex << std::showbase << c << '\n';
	return 0;
}

参考文献 编辑

  1. MSDN:Unicode Stream I/O in Text and Binary Modes