次の方法で共有


basic_filebuf クラス

型の特性を Trクラスにおける、型 Elem要素の送信を制御する外部ファイルに格納されている要素のシーケンスとしてストリーム バッファーを記述します。

template <class Elem, class Tr = char_traits<Elem> >
    class basic_filebuf : public basic_streambuf<Elem, Tr>

パラメーター

  • Elem
    ファイル バッファーの基本要素。

  • Tr
    ファイル バッファー (通常 char_traits<Elem>) の基本要素です。

解説

このテンプレート クラスは、外部ファイルに格納されている要素のシーケンスに対する型の特性を Trクラスにおける、型 Elem要素の送信を制御するストリーム バッファーを記述します。

注意

basic_filebuf オブジェクトは char_type に関係なく Elem型パラメーターによって指定された型 char * の内部バッファーによって作成されます。これは内部バッファーに書き込む前に、Unicode 文字列が (wchar_t の文字を含む) ANSI 文字列に変換できることを意味します。char の文字を含む)。バッファーの Unicode 文字列を格納するには、型 wchar_t の新しいバッファーを作成し、basic_streambuf::pubsetbuf() のメソッドを使用して設定します。この動作の例を参照するには、"参照してください。

クラス basic_filebuf<Elem、Tr> オブジェクト、ストリームが開いているファイルに関連付けられたコントロールに FILE オブジェクトを指定するファイルのポインターを格納します。 また、プロテクト メンバー関数 オーバーフローアンダーフローで使用するための 2 種類のファイルの変換のファセットへのポインターを格納します。 詳細については、「basic_filebuf::open」を参照してください。

使用例

次の例は、内部バッファーのストアの Unicode 文字に pubsetbuf() のメソッドを呼び出すことで、型 basic_filebuf<wchar_t> オブジェクトを変換する方法を示します。

// 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

たとえば

Elem テンプレート パラメーターに型名を関連付けます。

int_type

Tr のスコープ内の同じ名前の種類に basic_filebuf のスコープの等価内のこの型を作成します。

off_type

Tr のスコープ内の同じ名前の種類に basic_filebuf のスコープの等価内のこの型を作成します。

pos_type

Tr のスコープ内の同じ名前の種類に basic_filebuf のスコープの等価内のこの型を作成します。

traits_type

Tr テンプレート パラメーターに型名を関連付けます。

メンバー関数

閉じる

ファイルを閉じます。

is_open

ファイルが開いているかどうかを示します。

開く

ファイルを開きます。

オーバーフロー

新しい文字が完全なバッファーに挿入されたときに呼び出すことができる保護されたな仮想関数です。

pbackfail

プロテクト仮想メンバー関数関数は入力ストリームに要素を返すことを試みますと、現在の要素を (通常ポインタが指す)。

seekoff

プロテクト仮想メンバー関数関数はステートメント ストリームの現在位置を変更します。

seekpos

プロテクト仮想メンバー関数関数はステートメント ストリームの現在位置を変更します。

setbuf

プロテクト仮想メンバー関数関数は、各派生のストリーム バッファーに操作の両方を実行します。

交換

basic_filebuf 指定されたパラメーターの内容にこの basic_filebuf の内容を交換します。

同期

保護された、仮想関数は、関連する外部ストリームとステートメント ストリームを同期しようとします。

uflow

保護された入力ストリームから現在の要素を取得する仮想関数です。

アンダーフロー

保護された入力ストリームから現在の要素を取得する仮想関数です。

必要条件

ヘッダー: の <fstream>

名前空間: std

参照

関連項目

C++ 標準ライブラリ内のスレッド セーフ

iostream プログラミング

iostreams の規則

その他の技術情報

<fstream> メンバー

basic_filebuf のメンバー