/////////////////////////////////////////////////////////////////// // format.cpp - Solution of MT#1-Q3, MT#2-Q3, and MT#3-Q10 // // // // This is a demo showing how to save a stream's format state // // and justify with a manipulator. // /////////////////////////////////////////////////////////////////// #include typedef std::ios sios; typedef std::ostream ostr; // template argument allows you to save more than one stream state template class iostate { public: static void stream(sios& stream) { _pStream = &stream; } static void save() { if(_pStream) _state = _pStream->flags(); } static void restore() { if(_pStream) _pStream->flags(_state); } private: static long _state; static sios* _pStream; }; template sios* iostate::_pStream = 0; template long iostate::_state = 0; template ostr& Left(ostr &out) { out.setf(ostr::left,ostr::adjustfield); out.width(w); return out; } template ostr& Right(ostr &out) { out.setf(ostr::right,ostr::adjustfield); out.width(w); return out; } using namespace std; int main() { std::cout << "\n stream state = " << cout.flags(); iostate<1>::stream(std::cout); iostate<1>::save(); std::cout.fill('.'); std::cout << "\n " << Left<15> << 3.1415927; std::cout << "\n stream state = " << cout.flags(); std::cout << "\n " << Right<10> << -33; std::cout << "\n stream state = " << cout.flags(); iostate<1>::restore(); std::cout << "\n stream state = " << cout.flags() << "\n\n"; return 0; }