rdbuf C++
来源:互联网 发布:中文字符串相似度算法 编辑:程序博客网 时间:2024/05/16 17:47
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++中使用的是底层的流缓冲,效率更高!
- rdbuf()
- linux C++:c++流操作----->rdbuf()
- linux C++:c++流操作----->rdbuf()
- rdbuf C++
- 关于rdbuf
- rdbuf的使用
- C++ 中的rdbuf()
- C++流 rdbuf()
- rdbuf流缓冲
- rdbuf,重定向
- c++ 一日一练:istream.rdbuf(ostringstream.rdbuf())无效!
- c++流缓冲学习---rdbuf()
- c++流缓冲学习---rdbuf()
- c++流缓冲学习---rdbuf()
- c++流缓冲学习---rdbuf()
- c++的流控制rdbuf
- c++ 复制文件,利用rdbuf
- rdbuf 在批量数据测试中的应用
- Oracle 找出最耗资源的 SQL
- java软件工程师面试题
- livecd cetos安装
- 中国天使投资集中北上广浙苏 天使日益年轻化
- 驱动支持select函数
- rdbuf C++
- hg客户端密码配置说明.docx
- 服务器编程入门(11)TCP并发回射服务器实现 - 单线程select实现
- forward和sendRedirect的区别
- nginx-maven-配置文件关键点
- 解决懒加载问题的过滤器
- 手机软件开发者的系统选择
- HDU 1312 Red and Black
- poi 操作excle小例子