What does ifstream::rdbuf() actually do?

前端 未结 3 1442
不知归路
不知归路 2020-12-06 04:45

I have the following code and it works pretty good (other than the fact that it\'s pretty slow, but I don\'t care much about that). It doesn\'t seem intuitive that this woul

相关标签:
3条回答
  • 2020-12-06 05:01

    iostream classes are just wrappers around I/O buffers. The iostream itself doesn't do a whole lot… mainly, it the provides operator>> formatting operators. The buffer is provided by an object derived from basic_streambuf, which you can get and set using rdbuf().

    basic_streambuf is an abstract base with a number of virtual functions which are overridden to provide a uniform interface for reading/writing files, strings, etc. The function basic_ostream<…>::operator<<( basic_streambuf<…> ) is defined to keep reading through the buffer until the underlying data source is exhausted.

    iostream is a terrible mess, though.

    0 讨论(0)
  • 2020-12-06 05:22

    Yes, it's specified in the standard and it's actually quite simple. rdbuf() just returns a pointer to the underlying basic_streambuf object for the given [io]stream object.

    basic_ostream<...> has an overload for operator<< for a pointer to basic_streambuf<...> which writes out the contents of the basic_streambuf<...>.

    0 讨论(0)
  • 2020-12-06 05:22

    A quick look at the source code shows that basic_ofstream is the wrapper around basic_filebuf.

    0 讨论(0)
提交回复
热议问题