- 我们使用STL编程的时候有时候会想到把一个流对象指向的内容用另一个流对象来输出,比如想把一个文件的内容输出到显示器上,我们可以用简单的两行代码就可以完成。
- 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; }
- rdbuf函数有两种调用方法
- basic_streambuf<Elem, Traits> *rdbuf( ) const;
- basic_streambuf<Elem, Traits> *rdbuf( basic_streambuf<E, T> *_Sb);
- 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++中使用的是底层的流缓冲,效率更高!
linux C++:c++流操作----->rdbuf()
最新推荐文章于 2025-02-06 18:14:54 发布