C++IO流

C++IO流


一、IO流

流:若干字节数据从一端到另一端叫做流,数据的流动
流类体系

  • 流对象
  • 流运算符>> <<

二、输入输出流

  • ostream类
    • cout
    • cerr
    • clog
    • cin
    • 字符类的处理
      • 正常的操作
      • 调用成员函数的方法
    • 格式控制符
      • 包含头文件iomanip
      • 常用的格式控制,一种是调用成员函数方式,一种流控制字符去做
        • 设置有效位数:setprecision(n)
        • 设置精度:fixed结合setprecision使用
  • istream 类 cin
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
using namespace std;
void testOstream()  //output
{
   
	//freopen()
	cout << "标准输出" << endl;			//重定向
	cerr << "标准错误输出" << endl;		//不能重定向
	clog << "标准错误输出" << endl;		//重定向为文件
	//字符类的处理
	cout.put('a');
	cout << 'a' << endl;
	char c = 'C';
	cout.put(c);
	cout << c << endl;
	cout.write("onetwothree", 5);	//指定长度,超过长度不做输出
	cout << endl;
	//输入
	cout.put(cin.get());
	cout << endl;
	//字符串
	while (cin.get() != '\n');
	while (getchar() != '\n');
	char str[20]="";
	cin.getline(str, 20);
	cout.write(str,20);
}

void testiomanip()
{
   
	//格式控制符
	//设置格式
	double pi = 34.12343;
	cout << "设置有效位数:" << setprecision(4) <<  pi <<endl;
	cout << "有效小数位"<< fixed << setprecision(4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值