• 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

  • 相关阅读:
    Android 采用HttpClient提交数据到服务器
    Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use.解决办法
    Android 字符乱码问题的处理
    Android 网络HTML查看器
    Android消息机制入门
    Android ANR产生的原理和如何避免
    Android 网络图片查看器
    Ubuntu下使用Git和GitHub
    定制保存top输出信息的格式详解
    linux的top命令参数详解
  • 原文地址:https://www.cnblogs.com/pang1567/p/3998916.html
Copyright © 2020-2023  润新知