【c++primer】第八章01——标准IO库

编写一个函数,其唯一的形参和返回值都是istream&类型,该函数应一直读取流直到文件结束符为止,并将内容输出到标准输出。最后重设流使其有效,并返回该流。


//get.h
//自定义头文件
//该函数用来读取流指导文件结束,并且把流输出
#include"stdafx.h"

#ifndef GET_H
#define GET_H
#include<iostream>

std::istream& get(std::istream &);

#endif


//get.cpp
//自定义的get实现文件、源文件
#include"stdafx.h"
#include"get.hpp"
//


std::istream & get(std::istream & in)
{
	int ival;
	while(in >> ival,!in.eof())
	{
		if(in.bad())//如果出现系统故障,抛异常
			throw std::runtime_error("IO stream corrupted");
		if(in.fail())
		{
			std::cerr << "bad data,try again";
			in.clear();//恢复流
			in.ignore(200,' ');//跳过类型非法的输入项
			continue;//继续读入数据
		}
		std::cout << ival << " ";//读入正常的话,输出数据
	}
	in.clear();
	return in;

}

#include "stdafx.h"
#include"get.hpp"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	double dval;
	get(cin);
	cin >> dval;
	cout << dval << endl;

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值