C++ 输入输出流 详解

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 int _tmain(int argc, _TCHAR* argv[])
 6 {
 7     double values[] = { 1.23, 35.36, 653.7, 4358.24 };
 8     for (int i = 0; i < 4; i++)
 9     {
10         cout.width(10);
11         cout << values[i] << 'n';
12     }
13     getchar();
14     return 0;
15 }

使用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 }

程序运行结果:

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

宽度设置

设置宽度后,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 }

程序运行结果:

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

精度设置

浮点的默认精度默认为六,如果需要修改,使用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 }

运行结果:

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

使用固定记数法

 1 #include "stdafx.h"
 2 #include <iomanip>
 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 << setiosflags(ios::fixed) << setprecision(1)
11         << values[i]
12         << endl;
13     getchar();
14     return 0;
15 }

运行结果:

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

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

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

运行结果:

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

输入输出数据到文件:

 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 }

运行结果:

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

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

 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 }

运行结果:

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

1 cout.flush()      //刷新缓冲区
2 cout.put()        //把字符写入流中
3 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 }

运行结果:

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

原文链接: https://www.cnblogs.com/ybqjymy/p/12516107.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/336016

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午10:25
下一篇 2023年3月1日 下午10:25

相关推荐