f1504.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:

C#编程

开发平台:

Visual C++

  1. //=====================================
  2. // f1504.cpp
  3. // 异常方式
  4. //=====================================
  5. #include<fstream>
  6. #include<iostream>
  7. using namespace std;
  8. //-------------------------------------
  9. void procFileName(string s);
  10. void procOpenMode(string s);
  11. void openIn(string s);
  12. void openOut(string s);
  13. //-------------------------------------
  14. int main(){
  15.   procFileName("iabc");
  16.   procFileName("oabc");
  17. }//------------------------------------
  18. void procFileName(string s){
  19.   try{
  20.     for(char c='0'; c<='9'; c++) procOpenMode(s + c+".txt");
  21.   }catch(string s){
  22.     cout<<"error opening "<<s<<" not existed.n";
  23.   }
  24. }//------------------------------------
  25. void procOpenMode(string s){
  26.   if(s[0]=='i') openIn(s);
  27.   else openOut(s);
  28. }//------------------------------------
  29. void openIn(string s){
  30.   ifstream in(s.c_str());
  31.   if(!in) throw s+" inFile";
  32.   for(string line; getline(in, line); cout<<line<<"n");
  33. }//------------------------------------
  34. void openOut(string s){
  35.   fstream out(s.c_str(),ios::in|ios::out|ios::ate);
  36.   if(!out) throw s+string(" outFile");
  37.   out<<s+" outFile is ok.n";
  38.   cout<<s+" is here.n";
  39. }//====================================
  40.