ifstream infile("test.txt");
cout << infile.rdbuf();
上面的代码就把infile流对象中的流重定向到标准输出cout上,您可以在屏幕上看到test.txt的内容。
下面的例子来自MSDN,清晰的描述了rdbuf函数的使用方法
// basic_ios_rdbuf.cpp // compile with: /EHsc #include <ios> #include <iostream> #include <fstream> int main( ) { using namespace std; ofstream file( "rdbuf.txt" ); streambuf *x = cout.rdbuf( file.rdbuf( ) ); cout << "test" << endl; // Goes to file cout.rdbuf(x); cout << "test2" << endl; }输出的结果是:
test2
rdbuf函数有两种调用方法
basic_streambuf<Elem, Traits> *rdbuf( ) const;1)无参数。返回调用者的流缓冲指针。
2)参数为流缓冲指针。它使调用者与参数(流缓冲指针)关联,返回自己当前关联的流缓冲区指针。
假如我们用C语言写一个文件复制程序,比如一个mp3文件,我们首先考虑的是C语言的文件输入输出功能,其思路是建一个指定大小缓冲区,我们从源文件中循环读取缓冲区大小的数据,然后写进目的文件。而在C++中,我们抛弃了这种用字符缓冲区的按字节复制的方法,因为这种方法看起来很繁琐,而且效率一点也不高。下面可以对比这两种方法(程序可以直接执行):
C:
#include<stdlib.h>
#include<stdio.h>
int main()
{
char buf[256];
FILE *pf1, *pf2;
if((pf1 = fopen("1.mp3", "rb")) == NULL)
{
printf("源文件打开失败\n");
return 0;
}
if((pf2 = fopen("2.mp3","wb")) == NULL)
{
printf("目标文件打开失败\n");
return 0;
}
while(fread(buf,1,256,pf1), !feof(pf1))
{
fwrite(buf,1,256,pf2);
}
fclose(pf1);
fclose(pf2);
return 0;
}
在C++中:
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
fstream fin("1.mp3",ios::in|ios::binary);
if(!fin.is_open())
{
cout << "源文件打开失败" << endl;
return 0;
}
fstream fout("2.mp3",ios::out|ios::binary);
if(! fin.is_open())
{
cout << "目标文件打开失败!" << endl;
return 0;
}
fout<<fin.rdbuf();
fin.close();
fout.close();
return 0;
}
看起来是不是清晰多了呢,这就是C++中的流缓冲的威力了,程序通过把源文件的流重定向到关联到目的文件的流对象,通过 fout<<fin.rdbuf();一句代码就完成了在C语言中的循环读写缓冲区的功能,而且C++中使用的是底层的流缓冲,效率更高!
seekg()/seekp()与tellg()/tellp()的用法详解
对输入流操作:seekg()与tellg()
对输出流操作:seekp()与tellp()
下面以输入流函数为例介绍用法:
seekg()是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。
对于第一个参数,可以是正负数值,正的表示向后偏移,负的表示向前偏移。而第二个参数可以是:
ios::beg:表示输入流的开始位置
ios::cur:表示输入流的当前位置
ios::end:表示输入流的结束位置
tellg()函数不需要带参数,它返回当前定位指针的位置,也代表着输入流的大小。
假设文件test。txt为以下内容:
hello,my world
name:hehonghua
date:20090902
程序为:
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;
int main()
{
ifstream in("test.txt");
assert(in);
in.seekg(0,ios::end); //基地址为文件结束处,偏移地址为0,于是指针定位在文件结束处
streampos sp=in.tellg(); //sp为定位指针,因为它在文件结束处,所以也就是文件的大小
cout<<"filesize:"<<endl<<sp<<endl;
in.seekg(-sp/3,ios::end); //基地址为文件末,偏移地址为负,于是向前移动sp/3个字节
streampos sp2=in.tellg();
cout<<"from file topoint:"<<endl<<sp2<<endl;
in.seekg(0,ios::beg); //基地址为文件头,偏移量为0,于是定位在文件头
cout<<in.rdbuf(); //从头读出文件内容
in.seekg(sp2);
cout<<in.rdbuf()<<endl; //从sp2开始读出文件内容
return 0;
}
则结果输出:
file size:
45
from file to point:
30
hello,my world
name:hehonghua
date:20090902
date:20090902