#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
ifstream sourcefile;
ofstream targettedfile;
char ch;
string file1, file2,new1;
cout<<"Enter source file name with extension (like files.txt) : ";
cin>>file1;
sourcefile.open(file1);
if(!sourcefile)
{
cout<<"Error in opening source file..!!";
exit(1);//wll terminate the program immediately after printing cout and wont execute code after this
}
cout<<"Enter target file name with extension (like filet.txt) : ";
cin>>file2;
targettedfile.open(file2,ios::app);
if(!targettedfile)
{
cout<<"Error in opening target file..!!";
sourcefile.close();
exit(2);
}
while(sourcefile)
{
sourcefile.get(ch);
targettedfile<<ch;
}
cout<<"File copied successfully..!!";
sourcefile.close();
targettedfile.close();
return 0;
}
/*get will read character by character and fileobject>> will read everything as a word
if i use>> it will copy everything as a single word ignoring white spaces and new line*/
No comments:
Post a Comment