C++ 学习总结(二十五)——流类库

一. 流类库的结构组成:

1.流类库都派生于同一个基类:  class  streambuf

派生类:   filebuf(文件缓存区)    strstreambuf(字符串缓存区)   stdiobuf(标准输入输出缓存区)


2.

基类:                                                          ios

派生类:                    istream                                                                  ostream

派生类:ifstream     istream_withassign  istrstream               ofstream     ostream_withassign  ostrstream   

         (文件读取类)    (重载运算符“=”)   (输入串流)              (文件输出流)                                  (输出串流) 

3.

基类:                              iostream

派生类:  fstream             strstream                  stdiostream

       (文件输入输出流)  (字符串输入输出流)   (标准输入输出流)


二. 流输入函数

#include<iostream>
#include<stdlib.h>
using namespace std;
 //输入cin函数
void main1()
{
	char ch;
	cin >> ch;
	cout <<(char)(ch-32);

	cin.get();
	cin.get();
}
// cin.get()函数  从键盘输入字符
//cout.put()函数

void main2()
{
	char ch=0;
	while ((ch=cin.get())!='\t')
	{
		cout.put(ch);
	}	
}
//cin.getline(首地址,长度范围)
void main3()
{
	char str[30] = { 0 };
	cin.getline(str, 10);//限定输入长度

	cout << str;

	system("pause");
}

三.输入输出流的控制

#include<iostream>
#include<iomanip> //控制输出流
using namespace std;
/*cout.write(首地址,长度范围) 输出范围限定*/
void main11()
{
	cout.put('A').put('B').put('C');
	char str[] = "123456789afcdefg";
	cout.write(str,10);	//限定输出范围
	cin.get();
}
//cout 八进制  十六进制输出	  
//hex oct 进制强制标识
void main22()
{
	int num = 01070; 
	cout << num << endl; //十进制输出
	cout << hex<<num<<endl;//十六进制输出
	cout << oct << num << endl;//八进制输出
	cout << num;
	cin.get();
}
//浮点数精确输出 
void main33()
{
	double db = 1.9888888888888888888;
	cout << db << endl;	// 默认显示小数点后六位
	cout << setprecision(15) << db;//默认显示小数点的精确度
	cin.get();
}
//输出宽度设置与填充字符设置
void main44()
{
	cout.width(20);//输出宽度设置,如果实际长度超过了设定长度,则按照实际长度输出。
	cout.fill('*');	//填充符号
	cout.setf(ios::left);//左对齐
	cout<< "hello world"<<endl;

	cout.width(20);//输出宽度设置,如果实际长度超过了设定长度,则按照实际长度输出。
	cout.fill('*');	//填充符号
	cout.setf(ios::right); //右对齐
	cout << "hello world" << endl;

	cout.setf(ios::right,ios::left);//清除进位标志,设置为右对齐

	cin.get();
}
//进制输入输出的切换
void main55()
{
	int num1;
	cin.setf(ios::hex,ios::basefield);//设置输入为十六进制,
	cin>> num1;
	cout.setf(ios::hex, ios::basefield);
	cout << num1<<endl;

	int num2;
	cin.setf(ios::dec, ios::basefield);//设置输入为十进制
	cin >> num2;
	cout.setf(ios::dec, ios::basefield);
	cout << num2<<endl;//输出为十进制

	int num3;			
	cin.setf(ios::oct, ios::basefield);//设置输入为八进制
	cin >> num3;
	cout.setf(ios::oct, ios::basefield);
	cout << num3<<endl;		  


	cin.get();
	cin.get();
	cin.ge
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值