basic_filebuf 類別
從在外部檔案中的項目順序描述控制 Elem型別元素傳輸,性格特性類別取決於 Tr的資料流緩衝區。
template <class Elem, class Tr = char_traits<Elem> >
class basic_filebuf : public basic_streambuf<Elem, Tr>
參數
Elem
檔案緩衝區的基本項目。Tr
檔案緩衝區 (通常是 char_traits<Elem>) 中的基礎項目的特性。
備註
樣板類別往返於外部檔案中的項目順序描述控制 Elem型別元素傳輸,性格特性類別取決於 Tr的資料流緩衝區。
![]() |
---|
basic_filebuf 型別的物件會以型別不論 char_type 指定型別 char * 內部緩衝區參數的 Elem。這表示 Unicode 字串 (包含 wchar_t 字元) 會轉換為 ANSI 字串 (包含 char 字元),在將內部緩衝區之前被寫入。使用 basic_streambuf::pubsetbuf() 方法,要儲存 Unicode 字串緩衝區,請建立 wchar_t 型別的新緩衝區並將其設為。若要查看示範這個行為的範例,請參閱以下內容。 |
類別 basic_filebuf<Elem, Tr> 物件儲存檔案指標,指定 FILE 物件資料流與開啟檔案的控制項。 它也會儲存指標兩個檔案轉換 Facet 供 protected 成員函式使用 溢位 和 反向溢位。 如需詳細資訊,請參閱basic_filebuf::open。
範例
下列範例會在其內部緩衝區示範如何強制型別 basic_filebuf<wchar_t> 物件儲存 Unicode 字元 (呼叫 pubsetbuf() 方法。
// unicode_basic_filebuf.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <memory.h>
#include <string.h>
#define IBUFSIZE 16
using namespace std;
void hexdump(const string& filename);
int main()
{
wchar_t* wszHello = L"Hello World";
wchar_t wBuffer[128];
basic_filebuf<wchar_t> wOutFile;
// Open a file, wcHello.txt, then write to it, then dump the
// file's contents in hex
wOutFile.open("wcHello.txt",
ios_base::out | ios_base::trunc | ios_base::binary);
if(!wOutFile.is_open())
{
cout << "Error Opening wcHello.txt\n";
return -1;
}
wOutFile.sputn(wszHello, (streamsize)wcslen(wszHello));
wOutFile.close();
cout << "Hex Dump of wcHello.txt - note that output is ANSI chars:\n";
hexdump(string("wcHello.txt"));
// Open a file, wwHello.txt, then set the internal buffer of
// the basic_filebuf object to be of type wchar_t, then write
// to the file and dump the file's contents in hex
wOutFile.open("wwHello.txt",
ios_base::out | ios_base::trunc | ios_base::binary);
if(!wOutFile.is_open())
{
cout << "Error Opening wwHello.txt\n";
return -1;
}
wOutFile.pubsetbuf(wBuffer, (streamsize)128);
wOutFile.sputn(wszHello, (streamsize)wcslen(wszHello));
wOutFile.close();
cout << "\nHex Dump of wwHello.txt - note that output is wchar_t chars:\n";
hexdump(string("wwHello.txt"));
return 0;
}
// dump contents of filename to stdout in hex
void hexdump(const string& filename)
{
fstream ifile(filename.c_str(),
ios_base::in | ios_base::binary);
char *ibuff = new char[IBUFSIZE];
char *obuff = new char[(IBUFSIZE*2)+1];
int i;
if(!ifile.is_open())
{
cout << "Cannot Open " << filename.c_str()
<< " for reading\n";
return;
}
if(!ibuff || !obuff)
{
cout << "Cannot Allocate buffers\n";
ifile.close();
return;
}
while(!ifile.eof())
{
memset(obuff,0,(IBUFSIZE*2)+1);
memset(ibuff,0,IBUFSIZE);
ifile.read(ibuff,IBUFSIZE);
// corner case where file is exactly a multiple of
// 16 bytes in length
if(ibuff[0] == 0 && ifile.eof())
break;
for(i = 0; i < IBUFSIZE; i++)
{
if(ibuff[i] >= ' ')
obuff[i] = ibuff[i];
else
obuff[i] = '.';
cout << setfill('0') << setw(2) << hex
<< (int)ibuff[i] << ' ';
}
cout << " " << obuff << endl;
}
ifile.close();
}
建構函式
建構屬於 basic_filebuf 類型的物件。 |
Typedef
使型別名稱與 Elem 樣板參數。 |
|
認可 basic_filebuf 範圍內的對等型別為相同名稱的型別在 Tr 範圍的。 |
|
認可 basic_filebuf 範圍內的對等型別為相同名稱的型別在 Tr 範圍的。 |
|
認可 basic_filebuf 範圍內的對等型別為相同名稱的型別在 Tr 範圍的。 |
|
使型別名稱與 Tr 樣板參數。 |
成員函式
關閉檔案。 |
|
指出檔案是否為開啟狀態。 |
|
開啟檔案。 |
|
可以呼叫的受保護虛擬函式,當新的字元插入的緩衝區。 |
|
受保護的虛擬成員函式嘗試放回項目輸入資料流,然後再讓它目前項目 (指向下指標)。 |
|
受保護的虛擬成員函式嘗試修改控制項資料流中目前的位置。 |
|
受保護的虛擬成員函式嘗試修改控制項資料流中目前的位置。 |
|
受保護的虛擬成員函式執行這類作業專屬的每一個衍生自資料流緩衝區。 |
|
為提供的 basic_filebuf 參數的內容交換這個 basic_filebuf 的內容。 |
|
受保護的虛擬函式,嘗試同步處理與所有相關聯的外部資料流的控制流程。 |
|
受保護的虛擬函式,從輸入資料流擷取目前的項目。 |
|
受保護的虛擬函式,從輸入資料流擷取目前的項目。 |
需求
標題: <fstream>
命名空間: std