文件流习题8.3 8.4
生活随笔
收集整理的這篇文章主要介紹了
文件流习题8.3 8.4
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
將輸入內(nèi)容放入流中,并返回。
?
1 #ifndef _GET_H 2 #define _GET_H 3 4 #include <iostream> 5 6 std::istream& get(std::istream& in); 7 8 9 #endif 1 #include "get.h" 2 3 std::istream& get(std::istream& in) 4 { 5 6 int ival; 7 8 while(in >> ival, !in.eof()) 9 { 10 11 if(in.bad()) 12 throw std::runtime_error("IO stream corrupted"); 13 if(in.fail()) 14 { 15 std::cerr << "bad data. try again!" << std::endl; 16 in.clear(); 17 in.ignore(200, '\n'); 18 continue; 19 } 20 std::cout << "輸入的數(shù)據(jù):" << ival << std::endl; 21 22 } 23 in.clear(); 24 return in; 25 } 1 #include "get.h" 2 #include <iostream> 3 4 using namespace std; 5 6 int main() 7 8 { 9 cout << "測試所寫的函數(shù)" << endl; 10 11 double dval; 12 13 get(cin); 14 15 cout << "繼續(xù)使用cin,輸入一個(gè)double:" << endl; 16 cin >> dval; 17 cout << "你輸入的是:" << dval << endl; 18 19 return 0; 20 }?
轉(zhuǎn)載于:https://www.cnblogs.com/uniquews/archive/2012/12/23/2830024.html
總結(jié)
以上是生活随笔為你收集整理的文件流习题8.3 8.4的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何实现DataGridView实时更新
- 下一篇: linux 系统中的文件权限