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