linux C+:c++フロー操作--->rdbuf()

2064 ワード

    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++           ,    !