共用方式為


basic_filebuf Class

描述控制型別的項目傳輸資料流緩衝區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 *不論char_type型別參數所指定的Elem。這表示 Unicode 字串 (包含wchar_t字元) 將被轉換成 ANSI 字串 (包含char字元) 才能寫入至內部緩衝區。若要儲存在緩衝區中的 Unicode 字串,建立新的緩衝區類型的wchar_t ,並設定它使用basic_streambuf::pubsetbuf()方法。若要查看範例示範這種情形,請參閱底下。

An object of class basic_filebuf<Elem, Tr> 儲存檔案的指標,指定其FILE控制已開啟的檔案相關聯的資料流物件。 它還存放受保護的成員函式所使用的兩個檔案轉換 facet 的指標溢位反向溢位。 如需詳細資訊,請參閱 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();
}
  
  
  
  

tzf8k3z8.collapse_all(zh-tw,VS.110).gif建構函式

basic_filebuf

建構型別的物件basic_filebuf

tzf8k3z8.collapse_all(zh-tw,VS.110).gifTypedef

char_type

將相關聯的型別名稱Elem樣板參數。

int_type

可在這種類型basic_filebuf的範圍中相同名稱的型別等位Tr範圍。

off_type

可在這種類型basic_filebuf的範圍中相同名稱的型別等位Tr範圍。

pos_type

可在這種類型basic_filebuf的範圍中相同名稱的型別等位Tr範圍。

traits_type

將相關聯的型別名稱Tr樣板參數。

tzf8k3z8.collapse_all(zh-tw,VS.110).gif成員函式

關閉

關閉檔案。

is_open

指出檔案是否處於開啟狀態。

開啟

開啟檔案。

Overflow - 溢位

受保護虛擬函式可在新的字元會被插入到已滿的緩衝區時呼叫。

pbackfail

受保護的虛擬成員函式會嘗試輸入的資料流,放回到項目,然後讓它成為目前的元素 (由指向下一個指標)。

seekoff

受保護的虛擬成員函式會試著改變受控制的資料流的目前位置。

seekpos

受保護的虛擬成員函式會試著改變受控制的資料流的目前位置。

setbuf

受保護的虛擬成員函式在執行特定每一個衍生的資料流緩衝區的作業。

交換

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

同步處理

受保護的虛擬函式會嘗試與任何相關聯的外部資料流中同步處理受控制的資料流。

uflow

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

反向溢位

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

需求

標頭: <fstream>

Namespace: 標準

請參閱

參考

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

iostream 程式設計

iostreams 慣例

其他資源

<fstream> 成員

basic_filebuf 成員