有一个"文件打不开的异常处理程序":
#include<fstream.h>
#include<iostream.h>
#include<stdlib.h>
void main(int argc,char **argv){
ifstream f(argv[1]);
char buf[128];
try{
if(f.fail())
throw argv[1];
}
catch(char *c){
cout<<"error opening this file"<<c<<endl;
exit(1);
}
while(!f.eof()){
f.getline(buf,sizeof(buf));
cout<<buf<<endl;
}
f.close();
}
书上的说:
执行程序,假定D盘中没有my.txt文件,有you.txt文件
D:\>openfile my.txt
error opening the file my.txt
D:\>openfile you.txt
Good morning!
Good afternoon!(假设you.txt文件中的内容为这些句子)
但是我执行程序后出现一个对话框
内存"*************"不能read
要中止程序,请按"确定"
要调试程序,请按"取消"
#include<fstream.h>
#include<iostream.h>
#include<stdlib.h>
void main(int argc,char **argv){
ifstream f(argv[1]);
char buf[128];
try{
if(f.fail())
throw argv[1];
}
catch(char *c){
cout<<"error opening this file"<<c<<endl;
exit(1);
}
while(!f.eof()){
f.getline(buf,sizeof(buf));
cout<<buf<<endl;
}
f.close();
}
书上的说:
执行程序,假定D盘中没有my.txt文件,有you.txt文件
D:\>openfile my.txt
error opening the file my.txt
D:\>openfile you.txt
Good morning!
Good afternoon!(假设you.txt文件中的内容为这些句子)
但是我执行程序后出现一个对话框
内存"*************"不能read
要中止程序,请按"确定"
要调试程序,请按"取消"
一切正常!
建议main的返回值为int,正常返回0,异常非0;