共用方式為


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

建構屬於 basic_filebuf 類型的物件。

Typedef

char_type

使型別名稱與 Elem 樣板參數。

int_type

認可 basic_filebuf 範圍內的對等型別為相同名稱的型別在 Tr 範圍的。

off_type

認可 basic_filebuf 範圍內的對等型別為相同名稱的型別在 Tr 範圍的。

pos_type

認可 basic_filebuf 範圍內的對等型別為相同名稱的型別在 Tr 範圍的。

traits_type

使型別名稱與 Tr 樣板參數。

成員函式

關閉

關閉檔案。

is_open

指出檔案是否為開啟狀態。

開啟

開啟檔案。

Overflow - 溢位

可以呼叫的受保護虛擬函式,當新的字元插入的緩衝區。

pbackfail

受保護的虛擬成員函式嘗試放回項目輸入資料流,然後再讓它目前項目 (指向下指標)。

seekoff

受保護的虛擬成員函式嘗試修改控制項資料流中目前的位置。

seekpos

受保護的虛擬成員函式嘗試修改控制項資料流中目前的位置。

setbuf

受保護的虛擬成員函式執行這類作業專屬的每一個衍生自資料流緩衝區。

切換

為提供的 basic_filebuf 參數的內容交換這個 basic_filebuf 的內容。

同步處理

受保護的虛擬函式,嘗試同步處理與所有相關聯的外部資料流的控制流程。

uflow

受保護的虛擬函式,從輸入資料流擷取目前的項目。

反向溢位

受保護的虛擬函式,從輸入資料流擷取目前的項目。

需求

標題: <fstream>

命名空間: std

請參閱

參考

C++ 標準程式庫中的執行緒安全

iostream 程式設計

iostreams 慣例

其他資源

<fstream> 成員

basic_filebuf 成員