Aviad Rozenhek
Aviad Rozenhek

Reputation: 2409

what are the semantics of an iostream statement like myoutput << myinput.rdbuf() in c++?

take this code an example

std::ifstream myinput("myinput.txt");
std::ofstream myoutput("myoutout.txt");
myoutput << myinput.rdbuf();
  1. what are the semantics of the operation?
  2. C++ version is this specific overload of operator<< available in?
  3. is there a more explicit way [by using a named function instead of operator] to achieve the same effect?
  4. how can I tell how many bytes where copied in the operation?
  5. can I limit the number of bytes copied?
  6. can you outline how this specific overload of operator<< is implemented or point to an established implementation?

Upvotes: 1

Views: 125

Answers (1)

user1610015
user1610015

Reputation: 6678

  1. It copies the entire input stream (the entire file) to the output stream.
  2. It's always been available in C++. std::ifstream is defined in <istream> and std::ofstream is defined in <ostream>.
  3. Nope, that's the only way.
  4. Use myoutput.tellp().
  5. You'll need to read from the stream manually in a loop using std::ifstream::read, copying data into a buffer and writing that data to the output stream using std::ofstream::write, until you've copied the amount you want.
  6. If you have a C++ compiler installed, you can look at its implementation of it (most of the stream classes are templates, which means that their implementation must be in the header). For std::basic_ios::rdbuf, you can look in <ios>, and for std::ostream::operator<<(std::basic_streambuf*), you can look in <ostream>.

Upvotes: 3

Related Questions