首页 > 代码库 > C++ IO 格式控制器

C++ IO 格式控制器

1、不需要参数的IO控制器的函数定义在<iosteram>中,其中包括dec,oct和hex.也包括ws,endl,ends和flush以及如下图所示的内容。

image

 

2、需要参数的控制器定义在<iomainip>头文件中,有如下的预定义的控制器

image

3、下边是使用IO控制器的例子程序

   1:  #include <fstream>
   2:  #include <iomanip>
   3:  using namespace std;
   4:   
   5:  int main() {
   6:    ofstream trc("trace.out");
   7:    int i = 47;
   8:    float f = 2300114.414159;
   9:    char* s = "Is there any more?";
  10:   
  11:    trc << setiosflags(
  12:           ios::unitbuf /*| ios::stdio */ /// ?????
  13:           | ios::showbase | ios::uppercase
  14:           | ios::showpos);
  15:    trc << i << endl; // Default to dec
  16:    trc << hex << i << endl;
  17:    trc << resetiosflags(ios::uppercase)
  18:      << oct << i << endl;
  19:    trc.setf(ios::left, ios::adjustfield);
  20:    trc << resetiosflags(ios::showbase)
  21:      << dec << setfill(‘0‘);
  22:    trc << "fill char: " << trc.fill() << endl;
  23:    trc << setw(10) << i << endl;
  24:    trc.setf(ios::right, ios::adjustfield);
  25:    trc << setw(10) << i << endl;
  26:    trc.setf(ios::internal, ios::adjustfield);
  27:    trc << setw(10) << i << endl;
  28:    trc << i << endl; // Without setw(10)
  29:   
  30:    trc << resetiosflags(ios::showpos)
  31:      << setiosflags(ios::showpoint)
  32:      << "prec = " << trc.precision() << endl;
  33:    trc.setf(ios::scientific, ios::floatfield);
  34:    trc << f << endl;
  35:    trc.setf(ios::fixed, ios::floatfield);
  36:    trc << f << endl;
  37:    trc.setf(0, ios::floatfield); // Automatic
  38:    trc << f << endl;
  39:    trc << setprecision(20);
  40:    trc << "prec = " << trc.precision() << endl;
  41:    trc << f << endl;
  42:    trc.setf(ios::scientific, ios::floatfield);
  43:    trc << f << endl;
  44:    trc.setf(ios::fixed, ios::floatfield);
  45:    trc << f << endl;
  46:    trc.setf(0, ios::floatfield); // Automatic
  47:    trc << f << endl;
  48:   
  49:    trc << setw(10) << s << endl;
  50:    trc << setw(40) << s << endl;
  51:    trc.setf(ios::left, ios::adjustfield);
  52:    trc << setw(40) << s << endl;
  53:   
  54:    trc << resetiosflags(
  55:           ios::showpoint | ios::unitbuf
  56:           // | ios::stdio // ?????????
  57:   );
  58:  } ///:~
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

4、输入如下

+47
0X2F
057
fill char: 0
+470000000
0000000+47
+000000047
+47
prec = 6
2.300115e+006
2300114.500000
2.30011e+006
prec = 20
2300114.5000000000000
2.30011450000000000000e+006
2300114.50000000000000000000
2300114.5000000000000
Is there any more?
0000000000000000000000Is there any more?
Is there any more?0000000000000000000000

C++ IO 格式控制器