码迷,mamicode.com
首页 > 编程语言 > 详细

C++ IO 格式控制器

时间:2014-09-28 23:07:35      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   文件   

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

bubuko.com,布布扣

 

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

bubuko.com,布布扣

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

C++ IO 格式控制器

标签:style   blog   http   color   io   os   使用   ar   文件   

原文地址:http://www.cnblogs.com/pang1567/p/3998916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!