c++流緩衝學習—rdbuf()

來源:互聯網
上載者:User

 我們使用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++中使用的是底層的流緩衝,效率更高!

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.