Question by ~s.o.s~
Write a program which will perform the job of moving the file from one location to another. The source and destination path will be entered by the user. Perform the required error checking and handle the exceptions accordingly. (Intermediate)
#include<fstream>
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
fstream a,b;
char f1[100],f2[100],x[100];
cout<<"Enter file1 path";
cin.getline(f1,100);
cout<<"Enter file2 path";
cin.getline(f2,100);
try {
a.open(f1,ios::in|ios::binary);
if(!a)
throw 1;
try {
b.open(f2,ios::out|ios::binary);
if(!b)
throw 1;
while(!a.eof()){
a.getline(x,100);
b<<x<<"\n";
}
}catch(int i) {
cout<<"Error Writing file";
cin.get();
cin.get();
return -1;
}
}catch(int i) {
cout<<"Error opening File";
cin.get();
cin.get();
return -1;
}
a.close();
b.close();
try{
if(remove(f1))
throw 1;
cout<<"File Moved";
}catch(int i) {
cout<<"Error moving file";
cin.get();
cin.get();
return -1;
}
cin.get();
cin.get();
return 0;
}
What is the complexity of the above code?
Any corrections, comments or suggestions are welcomed :)