[Turn]c++ Stream Buffer---rdbuf ()

Source: Internet
Author: User
Tags file copy

The C + + standard library encapsulates a buffer class streambuf for use by input and output stream objects. Each standard C + + output stream object contains a pointer to streambuf that the user can obtain by calling the Rdbuf () member function to directly access the underlying Streambuf object. Therefore, you can read and write data directly to the underlying buffer, skipping the upper layer of formatted input and output operations.
For the file stream class and the string stream class, the corresponding stream buffer type is derived separately, referring to the following code:
The stream object obtains a pointer to the underlying STREAMBUF object by calling Rdbuf (), and the pointer calls Streambuf to support your various operations for input and output. This article does not discuss these operations, here is the main introduction of how to use this pointer to achieve the output of the file content.
The output stream provides an overloaded version of OPERATOR<<, which implements the output of all characters in the Streambuf object to the output outflow with the streambuf pointer, and the input stream also provides a corresponding operator>> overloaded version, Enter all the characters from the input stream object into the Streambuf object. The get member overloaded version of the input stream also has a version with the STREAMBUF pointer as the parameter, or it can be used to write something from the input stream into the output stream buffer.

The following three ways to implement the content of a file output standard output (of course, can also be done by ordinary standard format input and output):
Law one: through operator<<

#include <iostream>usingnamespaceint  main () {Ifstream fin (  "source.dat"); cout<<fin.rdbuf ();//guess actually the cached data carrier is a char*, and then overload the << operator return0

Method Two: Using the Get member function

 ifstream fin ( " source.dat   " ); What parameters are passed into the get inside, the content of the stream is entered into what, this is the basic concept of the input stream  while  (!fin.get  (*cout.rdbuf ()). EOF ()) {//  Extract a line input  if  (Fin.fail ()) //   blank line  fin.clear (); cout  < <char  (Fin. ()); //  extract ' \ n ' } 

Code interpretation: Because the Get version in the above code encounters the ' \ n ' character, it ends the extraction, so the output of the entire file content needs to be implemented in a loop. Additionally, when this version of the Get function encounters a blank line, because no characters are extracted (note: Get does not extract a carriage return), note that the FAIL flag ios::failbit is set, so you should call the clear () function to clear the error flag at this time. Similarly, because this version of Get does not extract carriage returns, you need to extract the carriage return with another version of Get ().

Method Three: Take advantage of overloaded get functions

Ifstream Fin ("main.cpp"); Fin. Get (*cout.rdbuf (), EOF);

Code explanation: This version of the Get member function can be customized to extract the Terminator. This is done by setting the file Terminator (EOF) to the purpose of extracting the entire file.

Of course, you can replace the above cout with any output stream, such as the file output stream, so that you can achieve the file copy function.

In addition, the >> operator of the input stream is not used in the above code, because >> and << are relative, just swap the operands for a position. So you can put the above code in out<<

[Turn]c++ Stream Buffer---rdbuf ()

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.