描述控制的元素类型的传输流缓冲区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();
}
构造函数
构造类型的对象basic_filebuf。 |
Typedef
将关联的类型名称与Elem模板参数。 |
|
使这种类型中的basic_filebuf的作用域中具有相同名称的类型与Tr作用域。 |
|
使这种类型中的basic_filebuf的作用域中具有相同名称的类型与Tr作用域。 |
|
使这种类型中的basic_filebuf的作用域中具有相同名称的类型与Tr作用域。 |
|
将关联的类型名称与Tr模板参数。 |
成员函数
关闭文件。 |
|
指示文件是否处于打开状态。 |
|
打开一个文件。 |
|
整个缓冲区中插入新的字符时,可以调用受保护虚函数。 |
|
受保护的虚拟成员函数将尝试将元素放回输入流,然后使其成为当前元素 (指向的指针下一步)。 |
|
受保护的虚拟成员函数试图改变控制流的当前位置。 |
|
受保护的虚拟成员函数试图改变控制流的当前位置。 |
|
受保护的虚拟成员函数执行的操作特定的每个派生的流缓冲区。 |
|
交换的内容basic_filebuf内容所提供的basic_filebuf参数。 |
|
受保护的虚拟函数尝试使用任何关联的外部流同步的控制的流。 |
|
受保护,从输入流中提取当前元素的虚函数。 |
|
受保护,从输入流中提取当前元素的虚函数。 |
要求
标题: <fstream>
命名空间: 标准