C ++ stream buffer learning-rdbuf ()

Source: Internet
Author: User

When we use STL programming, we sometimes think of outputting the content pointed to by a stream object with another stream object. For example, we want to output the content of a file to the display, we can use two simple lines of code.

Ifstream infile ("test.txt ");

Cout <infile. rdbuf ();

The exported code redirects the streams in the infilestream object to the standard output cout. you can view the content of test.txt on the screen.

The following example is from msdn, which clearly describes how to use the rdbuf function.

// 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; 
}

 

The rdbuf function can be called in two ways.

basic_streambuf<Elem, Traits> *rdbuf( ) const; 
basic_streambuf<Elem, Traits> *rdbuf( basic_streambuf<E, T> *_Sb);

 

1) No parameters. Returns the buffer pointer of the caller.

2) The parameter is a stream buffer pointer. It associates the caller with the parameter (stream buffer pointer) and returns the currently associated stream buffer pointer.

If we use the C language to write a file replication program, such as an MP3 file, we first consider the C language file input and output function. The idea is to create a buffer with a specified size, we cyclically read the buffer size data from the source file and then write it into the target file. In C ++, we discard the byte replication method using the character buffer, because this method looks cumbersome and inefficient at all. The following two methods can be compared (the program can be executed directly ):

C:

# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Char Buf [256];
File * pf1, * pf2;
If (pf1 = fopen ("1.mp3", "rb") = NULL)
{
Printf ("failed to open the source file/N ");
Return 0;
}
If (pf2 = fopen ("2.mp3", "WB") = NULL)
{
Printf ("failed to open the target file/N ");
Return 0;
}
While (fread (BUF, 1,256, pf1 ),! Feof (pf1 ))
{
Fwrite (BUF, 1,256, pf2 );
}
Fclose (pf1 );
Fclose (pf2 );
Return 0;

}

In C ++:

# Include <fstream>
# Include <iostream>
Using namespace STD;
Int main ()
{
Fstream fin ("1.mp3", IOS: In | IOs: Binary );
If (! Fin. is_open ())
{
Cout <"failed to open source file" <Endl;
Return 0;
}
Fstream fout ("2.mp3", IOS: Out | IOs: Binary );
If (! Fin. is_open ())
{
Cout <"failed to open the target file! "<Endl;
Return 0;
}
Fout <fin. rdbuf ();
Fin. Close ();
Fout. Close ();
Return 0;
}

Does it seem much clearer? This is the power of the stream buffer in C ++. The program redirects the source file stream to the stream object associated with the target file, use fout <fin. rdbuf (); a piece of code completes the function of circular read/write buffer in C language, and uses the underlying stream buffer in C ++, which is more efficient!

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.