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

来源:互联网 发布:java 命令模式 实例 编辑:程序博客网 时间:2024/05/23 01:17

编写一个函数,其唯一的形参和返回值都是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