C++/Console/cout flag status

Материал из C\C++ эксперт
Перейти к: навигация, поиск

cout flags: showpoint, showpos

#include <iostream>
using namespace std;
int main( )
{
  cout.setf(ios::showpoint | ios::showpos, ios::showpoint);
  cout << 102340.0; 
  return 0;
}


Displays the status of the format flags.

#include <iostream>
using namespace std;
void showflags();
int main()
{
  // show default condition of format flags
  showflags();
  cout.setf(ios::oct | ios::showbase | ios::fixed);
  showflags();
  return 0; 
}
void showflags()
{
  ios::fmtflags f;
  f = cout.flags();  // get flag settings
  
  if(f & ios::skipws) cout << "skipws on\n";
  else cout << "skipws off\n";
  if(f & ios::left) cout << "left on\n";
  else cout << "left off\n";
  if(f & ios::right) cout << "right on\n";
  else cout << "right off\n";
  if(f & ios::internal) cout << "internal on\n";
  else cout << "internal off\n";
  if(f & ios::dec) cout << "dec on\n";
  else cout << "dec off\n";
  if(f & ios::oct) cout << "oct on\n";
  else cout << "oct off\n";
  if(f & ios::hex) cout << "hex on\n";
  else cout << "hex off\n";
  if(f & ios::showbase) cout << "showbase on\n";
  else cout << "showbase off\n";
  if(f & ios::showpoint) cout << "showpiont on\n";
  else cout << "showpoint off\n";
  if(f & ios::showpos) cout << "showpos on\n";
  else cout << "showpos off\n";
  if(f & ios::uppercase) cout << "uppercase on\n";
  else cout << "uppercase off\n";
  if(f & ios::scientific) cout << "scientific on\n";
  else cout << "scientific off\n";
  if(f & ios::fixed) cout << "fixed on\n";
  else cout << "fixed off\n";
  if(f & ios::unitbuf) cout << "unitbuf on\n";
  else cout << "unitbuf off\n";
  if(f & ios::boolalpha) cout << "boolalpha on\n";
  else cout << "boolalpha off\n";
  cout << endl;
}


Store cout flags, set to new ones and restore

#include <iostream>
using namespace std;
int main()
{
  ios::fmtflags f;
  f = cout.flags(); // store flags
  cout.unsetf(ios::dec);
  cout.setf(ios::showbase | ios::hex);
  cout << 100 << "\n";
  cout.flags(f); // reset flags
  return 0;
}


Using Manipulators to Format I/O

/*
Manipulator       Purpose                                  Input/Output
-----------------+----------------------------------------+---------------
boolalpha         Turns on boolapha flag.                  Input/Output
dec               Turns on dec flag.                       Input/Output
endl              Output a newline character and           Output 
                  flush the stream.
ends              Output a null.                           Output
fixed             Turns on fixed flag.                     Output
flush             Flush a stream.                          Output
hex               Turns on hex flag.                       Input/Output
internal          Turns on internal flag.                  Output
left              Turns on left flag.                      Output
nobooalpha        Turns off boolalpha flag.                Input/Output
noshowbase        Turns off showbase flag.                 Output
noshowpoint       Turns off showpoint flag.                Output
noshowpos         Turns off showpos flag.                  Output
noskipws          Turns off skipws flag.                   Input
nounitbuf         Turns off unitbuf flag.                  Output
nouppercase       Turns off uppercase flag.                Output
oct               Turns on oct flag.                       Input/Output
resetiosflags     Turn off the flags specified in f.       Input/Output
(fmtflags f)
right             Turns on right flag.                     Output
scientific        Turns on scientific flag.                Output
setbase(int base) Set the number base to base.             Input/Output
setfill(int ch)   Set the fill character to ch.            Output
setiosflags       Turn on the flags specified in f.        Input/output
(fmtflags f)
setprecision      Set the number of digits of precision.   Output
(int p)
setw(int w)       Set the field width to w.                Output
showbase          Turns on showbase flag.                  Output
showpoint         Turns on showpoint flag.                 Output
showpos           Turns on showpos flag.                   Output
skipws            Turns on skipws flag.                    Input
unitbuf           Turns on unitbuf flag.                   Output
uppercase         Turns on uppercase flag.                 Output
ws                Skip leading white space.                Input
*/