IO streambuf introduction

streambuf objects are in charge of providing reading and writing functionality to/from certain types of character sequences, such as external files or strings.

streambuf objects are usually associated with one specific character sequence, from which they read and write data through an internal memory buffer. The buffer is an array in memory which is expected to be synchronized when needed with the physical content of the associated character sequence.

This is an abstract base class, therefore no objects can be directly instantiated from it. The standard hierarchy defines two classes derived from this one that can be used to directly instantiate objects: filebuf and stringbuf.

 

#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
 
 ifstream in ("C://Documents and Settings//gaoqian//Desktop//test.txt");
 int amount = 0;
 if (!in)
 {
  cout<<"input file error"<<endl;
  return -1;
 }
 
 while (!in.get(bs).eof())
 {
  if (in.fail())
   in.clear();
  cout<<char(in.get());
  ++amount;
  cout<<"run times is "<<amount<<"****"<<endl;
 }

 

 //another mean to implement this.
 ifstream in ("C://Documents and Settings//gaoqian//Desktop//test2.txt");
 cout<<in.rdbuf();
 
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章