C++输入输出流(十七)

32 篇文章 0 订阅
  1. 标准输入流
    1. cin.get() 获取一个字符
    2. cin.get(两个参数) 获取字符串
      1. 利用cin.get获取字符串时候,换行符遗留在缓冲区中
    3. cin.getline() 获取字符串
      1. 利用cin.getline获取字符串时候,换行符不会被取走,也不在缓冲区中,而是直接扔掉
    4. cin.ignore() 忽略 默认忽略1个字符, 如果填入参数X,代表忽略X个字符
    5. cin.peek()  偷窥 
    6. cin.putback()  放回 放回原位置
  2. 标准输出流
    1. cout.put() //向缓冲区写字符
    2. cout.write() //从buffer中写num个字节到当前输出流中。
    3. 通过 流成员函数 格式化输出
      1.       int number = 99;
      2.       cout.width(20); //指定宽度为20
      3.       cout.fill('*'); //填充
      4.       cout.setf(ios::left);  //左对齐
      5.       cout.unsetf(ios::dec); //卸载十进制
      6.       cout.setf(ios::hex);  //安装十六进制
      7.       cout.setf(ios::showbase);  //显示基数
      8.       cout.unsetf(ios::hex);  //卸载十六进制
      9.       cout.setf(ios::oct);   //安装八进制
      10.       cout << number << endl;
#include<iostream>
#include<iomanip>
using namespace std;

//cin.get() //一次只能读取一个字符
//cin.get(两个参数) //可以读字符串
//cin.getline()
//cin.ignore()
//cin.peek()
//cin.putback()

//cout.put() //向缓冲区写字符
//cout.write() //从buffer中写num个字节到当前输出流中。

void test01() {
	//char c = cin.get();
	//cout << c << endl;

	char str[1024];
	//cin.get(str,1024);
	//cout << str << endl;

	char e = cin.peek();
	cout << e << endl;

	cin.ignore();
	cin.ignore(2);

	char d=cin.peek();
	cout << d << endl;

	cin.getline(str ,1024);
	cout << str << endl;

	char f = cin.get();
	cin.putback(f);
	cout << "f=" << f << endl;

	char buf[1024] = { 0 };

	cin.getline(buf, 1024);
	cout << buf << endl;
}

void test02() {
	char c = cin.peek();
	char str[1024];
	cin.get(str, 1024);
	if (c >= '0' && c <= '9') {
		cout << "数字开头" << endl;
	}

	int c1;
	while (true) {
		cin >> c1;
		if (c1 >= 0&&c1<=9) {
			cout <<"c1=" << c1 << endl;
			break;
		}
		cin.clear();
		cin.sync();
		cin.ignore();
	}
}

void test03() {
	char c1 = 'a';
	cout.put(c1);

	char str[1024] = "char str[1024]=??????\n";
	cout.write(str,strlen(str));

	double number = 99.999;
	cout 
		<< setprecision(3) << fixed	//保留小数点后三位
		<< setw(20)     //设置宽度
		<< setfill('*')  //设置填充
		<< setiosflags(ios::showbase)  //显示基数
		<< setiosflags(ios::left)  //设置左对齐
		<< dec   //显示十进制,hex,dec,oct
		<< number
		<< endl;
}

int main() {
	//test01();
	//test02();
	test03();
	system("pause");
	return EXIT_SUCCESS;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值