C++输入输出流--<iostream>详解

C++输入输出流包含在头文件<iostream>中,

流的定义如下:
通过设备驱动程序与键盘、屏幕、文件、打印机等进行交互, iostream 类提供与之交互的方法。
输出流:
输出流的对象是字节目标,三个重要的输出流类是ostream、ofstream和ostringsream。
Ostream派生于basic_ostream支持预定义的流对象又:
cout标准输出
cerr标准错误输出,不经过缓冲
clog类似cerr,使用缓冲
注:缓冲是指将所有输出集中存放,然后一次性显示在屏幕上,避免多次刷屏。

格式控制
输出宽度:
输出宽度可以采用<iostream>中自带的width()函数,或者使用< iomanip >中的setw, setw 和宽度均不截断值。

使用width()函数代码如下:

 

 1 #include "stdafx.h"
 2 #include <iostream>   
 3 using namespace std;
 4 
 5 
 6 int _tmain(int argc, _TCHAR* argv[])
 7 {
 8     double values[] = { 1.23, 35.36, 653.7, 4358.24 };
 9     for (int i = 0; i < 4; i++)
10     {
11         cout.width(10);
12         cout << values[i] << '\n';
13     }
14     getchar();
15     return 0;
16 }

 

使用setw()函数

 

 1 #include "stdafx.h"
 2 #include <iostream>  
 3 #include <iomanip>
 4 using namespace std;
 5 
 6 int _tmain(int argc, _TCHAR* argv[])
 7 {
 8     double values[] = { 1.23, 35.36, 653.7, 4358.24 };
 9     for (int i = 0; i < 4; i++)
10     {
11         //cout.width(10);
12         cout << setw(10) << values[i] << '\n';
13     }
14     getchar();
15     return 0;
16 }

 

程序运行结果:

 

宽度设置

设置宽度后,cout默认空白填充,如果需要填充某个字符,可采用fill()或setfill()函数。
采用fill()函数

 

 1 #include "stdafx.h"
 2 #include <iostream>   
 3 using namespace std;
 4 
 5 
 6 int _tmain(int argc, _TCHAR* argv[])
 7 {
 8     double values[] = { 1.23, 35.36, 653.7, 4358.24 };
 9     for (int i = 0; i < 4; i++)
10     {
11         cout.width(10);
12         cout.fill('*');
13         cout << values[i] << '\n';
14     }
15     getchar();
16     return 0;
17 }

 

采用setfill()函数

 

 1 #include "stdafx.h"
 2 #include <iomanip>
 3 #include <iostream>  
 4 using namespace std;
 5 
 6 
 7 int _tmain(int argc, _TCHAR* argv[])
 8 {
 9     double values[] = { 1.23, 35.36, 653.7, 4358.24 };
10     for (int i = 0; i < 4; i++)
11     {
12         cout.width(10);
13         
14         cout << setfill('*') << values[i] << '\n';
15     }
16     getchar();
17     return 0;
18 }

 

程序运行结果:

 

精度设置

浮点的默认精度默认为六,如果需要修改,使用setprecision()。数字输出可以设置为固定型和科学型,输出形式采用setiosflags(ios::fixed)控制,fixed表示固定型,scientific表示科学型,默认为科学型。

科学型代码:

 

 1 #include "stdafx.h"
 2  
 3 #include <iostream>  
 4 using namespace std;
 5 
 6 int _tmain(int argc, _TCHAR* argv[])
 7 {
 8     double values[] = { 1.23, 35.36, 653.7, 4358.24 };
 9     for (int i = 0; i < 4; i++)
10         cout << setprecision(1)
11         << values[i]
12         << endl;
13     getchar();
14     return 0;
15 }

运行结果:

使用固定记数法

 

 1 #include "stdafx.h"
 2 #include <iomanip>
 3 #include <iostream>  
 4 using namespace std;
 5 
 6 
 7 int _tmain(int argc, _TCHAR* argv[])
 8 {
 9     double values[] = { 1.23, 35.36, 653.7, 4358.24 };
10     for (int i = 0; i < 4; i++)
11         cout << setiosflags(ios::fixed) << setprecision(1)
12         << values[i]
13         << endl;
14     getchar();
15     return 0;
16 }

 

运行结果:

将整形数字按照不同进制输出:

 

 1 #include "stdafx.h"
 2  
 3 #include <iostream>  
 4 using namespace std;
 5 
 6 
 7 int _tmain(int argc, _TCHAR* argv[])
 8 {
 9  
10     cout << 3536 << endl;//十进制
11     cout <<dec<< 3536 << endl;//十进制
12     cout << oct << 3536 << endl;//八进制
13     cout << hex << 3536 << endl;//十六进制
14 
15     getchar();
16     return 0;
17 }

运行结果:

 

输入输出数据到文件:

 

 1 #include "stdafx.h"
 2 #include <iostream>  
 3 #include <fstream>
 4 
 5 using namespace std;
 6 
 7 
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     ifstream ifile;
11     char buff[5] = { 0 };
12     ifile.open("d:/FILE1.txt", ios::in);
13     ifile.getline(buff, 5);
14     // Do some output  
15     ifile.close(); // FILE1 closed  
16 
17     cout << buff << endl;// 
18 
19     getchar();
20     return 0;
21 }

 

运行结果:

采用>>运算符读入整个字符串:

 

 1 #include "stdafx.h"
 2 #include <iostream>  
 3 #include <fstream>
 4 
 5 using namespace std;
 6 
 7 
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     ifstream ifile;
11     char buff[5] = { 0 };
12     ifile.open("d:/FILE1.txt", ios::in); 
13     ifile >> buff;
14     // Do some output  
15     ifile.close(); // FILE1 closed  
16 
17     cout << buff << endl;// 
18 
19     getchar();
20     return 0;
21 }

运行结果:

写入文件主要采用以下函数:

 

cout.flush()      //刷新缓冲区

 

  cout.put()        //把字符写入流中

 

  cout.write()      //将字符串写入当前输出流中

 

代码如下:

 1 #include "stdafx.h"
 2 #include <iostream>  
 3 #include <fstream>
 4 
 5 using namespace std;
 6 
 7 
 8 int _tmain(int argc, _TCHAR* argv[])
 9 {
10     ofstream ofile;
11     char buff[5] = { 0 };
12     ofile.open("d:/FILE1.txt", ios::in);
13     if (!ofile)
14     {
15         cout << "打开文件失败"  << endl;
16     }
17     ofile << "123" << endl;
18     ofile.write("xyz", 3);
19     ofile.put('M');
20     ofile.flush();//清空缓冲区
21     ofile.close(); // FILE1 closed  
22 
23     getchar();
24     return 0;
25 }

运行结果:

 

posted @ 2018-06-20 00:25  大尾巴贝贝  阅读(18567)  评论(0编辑  收藏  举报