c++中IO输入输出流总结<一>

1 io类图关系

1.1 简化形式

1.1.2补充

iostream:

  istream:从流中读取

  ostream:写入到流

  iosteram:读写流

fstream:

  ifstream:从文件读

  ofstream:写入文件

  fstream:读写文件

2 流综述

2.1什么情况会导致刷缓冲

  (1)程序正常结束,作为main函数结束的一部分将清空所有的缓冲区

  (2)缓冲区满会刷缓冲

  (3)endl flush刷缓冲

  (4)在每次输出操作执行完后用unitbuf操作符设置流内部状态从而清空缓冲区

2.2 标准输出

  输出流对象:cout,cerr,clog

2.2.1格式化输出iomanip

(1)相关控制符和功能如下图所示

(2)用格式控制符控制输出(记得包括头文件iomanip哦 下面也是)

 1 int main()
 2 {
 3     
 4     int n = 20;
 5     cout << "设置进制:" << endl;
 6     cout << "十进制" << n << endl;
 7     cout << "十六进制" << hex << n << endl;
 8     cout << "八进制" << oct << n << endl;
 9     cout << "十进制" << dec << n << endl;
10     cin.get();
11     return 1;
12 }

(3)设置域宽::::setw(n)n小于实际宽度按照实际宽度输出 

 1 int main()
 2 {
 3     int m = 1234;
 4     cout << "设置域宽" << endl;
 5     cout << setw(3) << m << endl;//1234
 6     cout << setw(5) << m << endl;// 1234
 7     cout << setw(10) << m << endl;//1234
 8     cin.get();
 9 
10     return 1;
11 }

(4)设置填充字符setfil。。。。。需要和setw一起使用 

 1 int main6()
 2 {
 3     int x = 1234;
 4     cout << "设置填充字符" << endl;
 5     cout << setfill('*')<<setw(5)<<x<< endl;//*1234
 6     cout << setw(10)<<x<< endl;// ******1234
 7     
 8     cin.get();
 9     return 1;
10 }

(5)设置对齐方式setiosflags(ios::left/right)

 1 int main7()
 2 {
 3     int y = 1234;
 4     cout << "设置对齐方式" << endl;
 5     cout << setfill(' ');
 6     cout << setiosflags(ios::left) << setw(10) << y << endl;
 7     cout << setiosflags(ios::right) << setw(10) << y << endl;
 8 
 9     cin.get();
10     return 1;
11 
12 }

(6)显示小数点和正负数符号

 1 int main8()
 2 {
 3     double d1 = 10 / 5, d2 = 22.0 / 7;
 4     cout << "显示小数点尾和数符" << endl;
 5     cout << d1 << endl;//2
 6     //2.00000
 7     cout << setiosflags(ios::showpoint) << d1 << endl;
 8     //+3.14286强制显示符号
 9     cout << setiosflags(ios::showpos) << d2 << endl;
10     //+3.14286
11     cout << d2 << endl;
12     cin.get();
13     return 1;
14 
15 }

(7)设置进度

 1 int main10()
 2 {
 3     double dd = 123.4567;
 4     //1.2e+002
 5     cout << setprecision(2) << dd << endl;
 6     //123
 7     cout << setprecision(3) << dd << endl;
 8     //123.5
 9     cout << setprecision(4) << dd << endl;
10     //123.46这里会四舍五入
11     cout << setprecision(5) << dd << endl;
12     cin.get();
13     return 1;
14 
15 }

3 标准输入cin

3.1 成员函数get

  char get() 输入一个字符并返回(回车 tab等)

  istream& get(char &)//实现链式编程 

1 int main()
2 {
3     char ch;
4     while(cin.get(ch))
5     {
6           cout.put(ch);
7     }              
8     return 1;
9 }

  istream& get(char*,int,char)//从输入流读取N-1字符,赋给字符数组

 情况1: 

 1 int main()
 2 {
 3     char buf[1024];
 4 
 5     cin.get(buf, 1024, '/');
 6     //cin.ignore(1);
 7     cout << buf << endl;
 8     cin.get(buf, 1024, '/');
 9 
10     cout << buf << endl;
11     system("pause");
12 }

备注:这里没有ignore 按照字符/截断了

情况2:丢失了c  

 1 int main()
 2 {
 3     char buf[1024];
 4 
 5     cin.get(buf, 1024, '/');//输入i love china/     i love tianchao/
 6     cin.ignore(100,'c');
 7     cout << buf << endl;
 8     cin.get(buf, 1024, '/');
 9 
10     cout << buf << endl;
11     system("pause");
12 }

情况三:退回到c

 1 int main()
 2 {
 3     char buf[1024];
 4 
 5     cin.get(buf, 1024, '/');
 6     cin.ignore(100,'c');//忽略到c字符位置 保存后面的值
 7     cin.putback('c');
 8     cout << buf << endl;
 9     cin.get(buf, 1024, '/');
10 
11     cout << buf << endl;
12     system("pause");
13 }

peek

 1 int main()
 2 {
 3     char buf[1024];
 4 
 5     cin.get(buf, 1024, '/');
 6     cin.ignore(100,'c');//忽略到c字符位置 保存后面的值
 7     cin.putback('c');//退回c
 8 
 9     char peek = cin.peek();
10     cout << "peek"<<peek << endl;
11 
12     cout << buf << endl;
13     cin.get(buf, 1024, '/');
14 
15     cout << buf << endl;
16     system("pause");
17 }

 

 参数:字符数组 字符个数终止字符

    注意:会清空char*指向的内容 如果没有读到n-1个字符或者终止符则会阻塞

3.2 getline

  istream& getline(char*,int,char)

     与get不同的是在读取n-1字符之前遇到终止字符会提前结束.两者最大不同是get当遇到定界符停止当时bu'hu

      cin.getline(buf,1024,'g'); 

原文地址:https://www.cnblogs.com/lanjianhappy/p/7289360.html