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控制与打开的文件关联的流对象。它还存储的受保护的成员函数的两个文件转换为使用小平面指向溢出下溢。有关更多信息,请参见 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-cn,VS.110).gif构造函数

basic_filebuf

构造类型的对象basic_filebuf

tzf8k3z8.collapse_all(zh-cn,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-cn,VS.110).gif成员函数

关闭

关闭文件。

is_open

指示文件是否处于打开状态。

打开

打开一个文件。

Overflow — 溢出

整个缓冲区中插入新的字符时,可以调用受保护虚函数。

pbackfail

受保护的虚拟成员函数将尝试将元素放回输入流,然后使其成为当前元素 (指向的指针下一步)。

seekoff

受保护的虚拟成员函数试图改变控制流的当前位置。

seekpos

受保护的虚拟成员函数试图改变控制流的当前位置。

setbuf

受保护的虚拟成员函数执行的操作特定的每个派生的流缓冲区。

换用

交换的内容basic_filebuf内容所提供的basic_filebuf参数。

同步

受保护的虚拟函数尝试使用任何关联的外部流同步的控制的流。

uflow

受保护,从输入流中提取当前元素的虚函数。

下溢

受保护,从输入流中提取当前元素的虚函数。

要求

标题: <fstream>

命名空间: 标准

请参见

参考

线程安全性对标准C++库中

iostream编程

(mfc)约定

其他资源

fstream 成员

basic_filebuf 成员