C++檔案流操作與流緩衝重新導向

來源:互聯網
上載者:User
 1 #include <iostream>
2 #include <fstream>
3 #include <cassert>
4
5 using namespace std;
6 int main()
7 {
8 ifstream in("test.txt");
9 assert(in.is_open());
10
11 //基地址為檔案結束處,位移地址為0,於是指標定位在檔案結束處
12 in.seekg(0, ios::end);
13 //sp為定位指標,因為它在檔案結束處,所以也就是檔案的大小
14 streampos sp = in.tellg();
15 cout<<"file size:"<<endl<<sp<<endl;
16
17 //基地址為檔案末,位移地址為負,於是向前移動sp/3個位元組
18 in.seekg(-sp/3, ios::end);
19 streampos sp2 = in.tellg();
20 cout<<"from file to point:"<<endl<<sp2<<endl;
21
22 //基地址為檔案頭,位移量為0,於是定位在檔案頭; 從頭讀出檔案內容
23 in.seekg(0, ios::beg);
24 cout<<in.rdbuf()<<endl;
25
26 //從sp2開始讀出檔案內容
27 in.seekg(sp2);
28 cout<<in.rdbuf()<<endl;
29
30 in.close();
31
32 return 0;
33 }

 

 1 #include <iostream>
2 #include <fstream>
3 #include <cassert>
4 #include <streambuf>
5
6 int main(void)
7 {
8 std::ofstream log("test.txt");
9 assert(log.is_open());
10
11 // 返回cout原來的的流緩衝指標,使cout重新導向到log檔案的流緩衝
12 std::streambuf* x = std::cout.rdbuf(log.rdbuf());
13
14 // 寫入到檔案中
15 std::cout << "xiaolouyiyetingchunyu\n";
16
17 log.close();
18
19 std::cout.rdbuf(x); // 恢複cout的流對象指標
20
21 std::cout << "yuanyuewandao\n"; // 寫入cout
22
23 return 0;
24 }

 我們使用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.