C++: I/O流详解(二)——输入输出格式控制

一、格式控制

ios提供直接设置标志字的控制格式函数

iostream和iomanip库还提供了一批控制符简化I/O格式化操作

 1 状态标志            值           含义                   输入/输出
 2 skipws          0X0001    跳过输入中的空白                  I
 3 left            0X0002    左对齐输出                       O
 4 right           0X0004    右对齐输出                   O
 5 internal        0X0008    在符号位和基指示符后填入字符      O
 6 dec             0X0010    转换基制为十进制                I/O
 7 oct             0X0020    转换基制为八进制                 I/O
 8 hex             0X0040    转换基制为十六进制             I/O
 9 showbase        0X0080    在输出中显示基指示符              O
10 showpoint       0X0100    输出时显示小数点                 O
11 uppercase       0X0200    十六进制输出时一律用大写字母       O
12 showpos         0X0400    正整数前加“+”号                 O
13 scientific      0X0800    科学示数法显示浮点数             O
14 fixed           0X1000    定点形式显示浮点数               O
15 unitbuf         0X2000    输出操作后立即刷新流             O
16 stdio           0X4000    输出操作后刷新stdout 和 stdree   O

设置标识字:

例1:

 1 //例10-4  设置输出宽度 
 2 #include <iostream.h>
 3 void main()
 4 { char *s = "Hello";
 5    cout.fill( '*' ) ;            // 置填充符
 6    cout.width( 10 ) ;        // 置输出宽度
 7    cout.setf( ios :: left ) ;        // 左对齐
 8    cout << s << endl ;
 9    cout.width( 15 ) ;        // 置输出宽度
10    cout.setf( ios :: right, ios :: left ) ;    // 清除左对齐标志位,置右对齐
11    cout << s << endl ;
12 } 

输出:

例二:不同基数形式的输入输出

 1 #include <iostream.h>
 2 void main()
 3 { int a , b , c ;
 4    cout << "please input a in decimal: " ;
 5    cin.setf( ios :: dec , ios :: basefield ) ;   cin >> a ;    //十进制输入
 6    cout << "please input b in hexadecimal: " ;        
 7    cin.setf( ios :: hex , ios :: basefield ) ;   cin >> b ;     //十六进制输入
 8    cout << "please input c in octal: " ;
 9    cin.setf( ios :: oct , ios :: basefield ) ;    cin >> c ;     //八进制输入
10    cout << "Output in decimal :
" ;
11    cout.setf( ios :: dec, ios :: basefield );            //十进制输出
12    cout << "a = " << a << "  b = " << b << "  c = " << c << endl ;
13    cout.setf( ios :: hex , ios :: basefield ) ;        //十六进制输出
14    cout << "Output in hexadecimal :
" ;
15    cout << "a = " << a << "  b = " << b << "  c = " << c << endl ;
16    cout.setf( ios :: oct , ios :: basefield ) ;        //八进制输出
17    cout << "Output in octal :
" ;
18    cout << "a = " << a << "  b = " << b << "  c = " << c << endl ;
19 }

输出:

例三:格式化输出浮点数

 1 #include <iostream.h>
 2 void main()
 3 { double x = 22.0/7 ;
 4    int i ;
 5    cout << "output in fixed :
" ;
 6    cout.setf( ios::fixed | ios::showpos ) ;         // 定点输出,显示 +
 7    for( i=1; i<=5; i++ )
 8       { cout.precision( i ) ;  cout << x << endl ; }
 9    cout << "output in scientific :
" ;
10    // 清除原有设置,科学示数法输出
11    cout.setf(ios::scientific, ios::fixed|ios::showpos ) ;    
12    for( i=1; i<=5; i++ )
13       { cout.precision(i) ;   cout << x*1e5 << endl ; }
14 }

二、格式控制符

 控制符是istream和ostream类定义了一批函数,作为重载插入运算符<<或提取运算符>>的右操作数控制I/O格式。

 

例1:

 1 // 整数的格式化输出
 2 #include <iostream>
 3 #include <iomanip>
 4 using namespace std ;
 5 void main()
 6 { const int k = 618 ;
 7    cout << setw(10) << setfill('#') << setiosflags(ios::right) << k <<endl ;
 8    cout << setw(10) << setbase(8) << setfill('*')
 9            << resetiosflags(ios::right) << setiosflags(ios::left) << k << endl ;
10 } 

输出:

原文地址:https://www.cnblogs.com/llxblogs/p/7768669.html