zoukankan      html  css  js  c++  java
  • 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:  } ///:~

    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

  • 相关阅读:
    30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用
    SAP Marketing Cloud功能简述(一) : Contacts和Profiles
    如何使用点击超链接的方式打开Android手机上的应用
    1036. Boys vs Girls (25)
    Yet another A + B
    1033. To Fill or Not to Fill (25)
    1032. Sharing (25)
    1021. Deepest Root (25)
    1017. Queueing at Bank (25)
    1016. Phone Bills (25)
  • 原文地址:https://www.cnblogs.com/pang1567/p/3998916.html
Copyright © 2011-2022 走看看