f1504.cpp

来自「it is a usefull thing」· C++ 代码 · 共 41 行

CPP
41
字号
//=====================================
// f1504.cpp
// 异常方式
//=====================================
#include<fstream>
#include<iostream>
using namespace std;
//-------------------------------------
void procFileName(string s);
void procOpenMode(string s);
void openIn(string s);
void openOut(string s);
//-------------------------------------
int main(){
  procFileName("iabc");
  procFileName("oabc");
}//------------------------------------
void procFileName(string s){
  try{
    for(char c='0'; c<='9'; c++) procOpenMode(s + c+".txt");
  }catch(string s){
    cout<<"error opening "<<s<<" not existed.\n";
  }
}//------------------------------------
void procOpenMode(string s){
  if(s[0]=='i') openIn(s);
  else openOut(s);
}//------------------------------------
void openIn(string s){
  ifstream in(s.c_str());
  if(!in) throw s+" inFile";
  for(string line; getline(in, line); cout<<line<<"\n");
}//------------------------------------
void openOut(string s){
  fstream out(s.c_str(),ios::in|ios::out|ios::ate);
  if(!out) throw s+string(" outFile");
  out<<s+" outFile is ok.\n";
  cout<<s+" is here.\n";
}//====================================

 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?