要求:

理解C++流操作函数

理解I/O流格式控制的两种方法


输出流(cout)基本操作

1、使用流插入运算符(<<)

  #include<iostream.h>

  void main()

  {

    char c[]="Good morning";

    cout<<c<<endl;   

  }

  运行结果:

  Good morning

  Press any key to continue

2、使用成员函数

  // ostream &put(char);
  // 输出一个字符,可连续使用

  #include<iostream.h>

  void main()

  {

    cout.put('A').put('\n');

  }

  运行结果:

  A

  Press any key to continue

  // ostream &write(const char*,int);
  // 输出若干个字符

  #include<iostream.h>

  void main()

  {

    char c[]="Good morning";

    cout.write(c,4)<<endl;   

  }

  运行结果:

  Good

  Press any key to continue

输入流(cin)基本操作

1、使用提取运算符(>>)

  //输入用空格、制表符、回车分隔

  #include<iostream.h>

  void main()

  {

    int i,j; char c;

    cin>>i>>c>>j;

    cout<<i<<"["<<c<<"]"<<j<<endl;

  }

  运行结果:

  123 456

  123[4]56

  Press any key to continue

2、使用成员函数

  // int get();
  // 读到结束标记返回EOF(-1)

  #include<iostream.h>

  void main()

  {

    while(true)

    {

      cout<<cin.get()<<'\\';

      if(cin.eof())break;

    }

  }

  运行结果:

  12 34  5

  49\50\32\51\52\9\53\10\^Z

  -1\Press any key to continue

  // istream &get(char&);
  // 读到结束标记返回NULL指针

  #include<iostream.h>

  void main()

  {

    char c;

    while(true)

    {

      cout<<cin.get(c)<<endl;

      if(cin.eof())break;

    }

  }

  运行结果:

  12

  0x004300EC

  0x004300EC

  0x004300EC

  ^Z

  0x00000000

  Press any key to continue

  //istream &get(char*,int n,char delimit='\n');
  //get不从流中取出结束符,默认结束符是回车
  #include<iostream.h>
  void main()
  {
    char s1[10],s2[10],s3[10];
    cin.get(s1,10);
    cin.get(s2,10);
    cin.get(s3,10);
    cout<<"---------------------\n";
    cout<<s1<<endl;
    cout<<s2<<endl;
    cout<<s3<<endl;
  }
  运行结果:
  1234567890ABCDEF
  ---------------------
  123456789
  0ABCDEF
  
  Press any key to continue
  //istream &getline(char*,int n,char delimit='\n');
  //getline将从流中取出结束符,默认结束符是回车

  #include<iostream.h>
  void main()
  {
    char s1[10],s2[10],s3[10];
    cin.getline(s1,10);
    cin.getline(s2,10);
    cin.getline(s3,10);
    cout<<"---------------------\n";
    cout<<s1<<endl;
    cout<<s2<<endl;
    cout<<s3<<endl;
  }
  1234567890ABCDEF
  Bye bye
  ---------------------
  123456789
  0ABCDEF
  Bye bye
  Press any key to continue

  //isrream &read(char *,int);

  #include<iostream.h>

  void main()

  {

    char c[10];

    cin.read(c,10);

    cout.write(c,cin.gcount())<<endl;

  }

  运行结果:

  1234567890ABCDEF

  1234567890

  Press any key to continue


2、I/O格式控制

ios类中有一些格式控制标志位,可通过公有函数来改变这些标志位

  long  flags() ;      //返回标志字的值

  long  flags(long);    //设置标志字的值

  long  setf(long);     //设置标志位

  long  setf(long,long);  //设置对齐、数制、浮点标志位

  long  unsetf(long);    //清除标志位

  int   width();

  int   width(int);     //无保持性,对紧随其后输出的数据有效

  char  fill();

  char  fill(chac);     //填充字符

  int   precision();

  int   precision(int);   //设置精度


设置标志位时,可使用ios类定义的枚举常量:

  enum { skipws   = 0x0001,

      left    = 0x0002,  // adjustfield
      right   = 0x0004,
      internal  = 0x0008,

      dec    = 0x0010,  // basefield
      oct    = 0x0020,
      hex    = 0x0040,

      showbase  = 0x0080,

      showpoint = 0x0100,

      uppercase = 0x0200,

      showpos  = 0x0400,

      scientific = 0x0800,  // floatfield
      fixed   = 0x1000,

      unitbuf  = 0x2000,

      stdio   = 0x4000

     };


用成员函数控制:

  #include<stdio.h>

  #include<iostream.h>

  void main()

  {

    int i=234;

    printf("%08X\n",i);

    cout.setf(ios::hex,ios::basefield);

    cout.setf(ios::uppercase);

    cout.width(8); 

    cout.fill('0');

    cout<<i<<endl;

    cout.setf(ios::dec,ios::basefield);

    cout<<i<<endl;

  }

  运行结果:

  000000EA

  000000EA

  234

  Press any key to continue


用流操纵算子控制格式

  算子可直接作为I/O输出表达式的一项,算子分为无参算子及有参算子

  算子分为无参算子  iostream.h

    dec           //十进制

    oct           //八进制

    hex           //16进制

    endl          //换行

    flush          //立即输出

  有参算子      iomanip.h

    resetiosflags(long)   //清除标志位

    setfill(int)      //设置填充字符

    setiosflags(long)    //设置标志位

    setprecision(int)    //设置精度

    setw(int)        //设置宽度

  //用流操纵算子控制格式

  #include<iostream.h>

  #include<iomanip.h>

  void main()

  {

    int i=234;

    cout<<hex<<setfill('0')<<setw(8)<<setiosflags(ios::uppercase)<<i<<endl;

    cout<<i<<endl;

    cout<<dec<<i<<endl;

    cout<<setw(8)<<i<<endl;

  }

  运行结果:

  000000EA

  EA

  234

  00000234

  Press any key to continue

显示浮点数

  //没指定是定点(fixed)还是科学(scientific)时,默认输出6位有效数字
  //指定后,精度是指小数点后的有效位数

  #include<iomanip.h>

  #include<stdio.h>

  #include<iostream.h>

  void main()

  {

    double d=123.4567890123456789123456789;

    cout<<d<<endl;             //默认6位有效数字

    cout<<setprecision(4)<<d<<endl;     //保留小数点后3位

    cout<<setiosflags(ios::fixed)<<d<<endl; //科学记数法

    cout<<setprecision(20)<<d<<endl;    //能显示20位吗?

    cout<<resetiosflags(ios::fixed);    //取消定点显示

    cout<<setiosflags(ios::scientific);   //科学记数法

    cout<<setprecision(4)<<d<<endl;     //保留小数点后2位

  }

  运行结果:

  123.457

  123.5

  123.4568

  123.456789012345680

  1.2346e+002

  Press any key to continue