Un saluto a tutti. Sto facendo pratica con la gestione delle eccezioni ma ho qualche problema. Vi posto il codice.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void error(ifstream source){
try {
if(source.fail()==true) throw 0;
if(source.bad()==true) throw 1;
}
catch(int n){
if(n==0){
cerr << "Errore!" << endl;
return -1;
} else {
cerr << "Errore grave!" << endl;
return -1;
}
}
cout << "Fine div" << endl;
}
int main(){
ifstream source;
ofstream destination;
source.open("source.srt", ios::in);
destination.open("destination.srt", ios::app);
error(source);
char riga[1024];
while(source.eof()==false){
error(source);
source.getline(riga, 1024);
destination << riga << endl;
}
source.close();
destination.close();
return 0;
}
Altra domanda, se volessi testare il bad() e il fail() separatamente come potrei fare???
Grazie anticipatamente.
|