< cpp‎ | io‎ | basic ios
Revision as of 10:45, 31 January 2013 by Cubbi (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl ">

<td class="t-dcl-nopad">
basic_ios& copyfmt(const basic_ios& other);

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Copies the state of the stream other into *this. This is done in the following sequence:

1) Calls every callback registered by register_callback() passing erase_event as parameter

2) Copies all member objects from other to *this except for Template:rlpf, the exception mask, and Template:rlpf. In particular, makes copies of the locale, the formatting flags, the contents of the arrays ios_base::iword and ios_base::pword (but not the iword and pword pointers themselves), the callbacks, and the tied stream.

3) Calls every callback registered by register_callback() passing copyfmt_event as parameter

4) Copies the exception mask from other to *this as if by calling exceptions(rhs.except())



other - another stream to use as source

Return value



The second pass through the callbacks may be used to deep-copy the user-defined objects pointed to by the pointers in ios_base::pword

copyfmt() may be used to save and restore the state of a stream. Boost provides a more fine-grained IO state savers library for the same purpose.


makes the ofstream object "out" behave exactly like std::cout, including formatting, tie() to std::cin, etc.

#include <iostream>
#include <fstream>
int main()
    std::ofstream out;
    out.copyfmt(std::cout); // copy everything except rdstate and rdbuf
    out.clear(std::cout.rdstate()); // copy rdstate
    out.basic_ios<char>::rdbuf(std::cout.rdbuf()); // share the buffer
    out << "Hello, world\n";


Hello, world