Pages

Tuesday, November 21, 2017

C++ Program to copy files.

#include<iostream>
#include<fstream>

using namespace std;
int main()
{
ifstream fin;
ofstream fout;
char ch, fname1[20], fname2[20];

fin >> std::noskipws; //To copy whitespace characters

cout<<"Enter source file name: ";
gets(fname1);
fin.open(fname1);

if(!fin){
cout<<"Error Open Error.";
exit(1);
}

cout<<"Enter target file name: ";
gets(fname2);
fout.open(fname2);

if(!fout){
cout<<"File Open Error.";
exit(1);
}
//Logic to copy data from file1 to file2
while(fin.eof()==0){
fin >> ch;
fout << ch;
}
cout<<"File content copied successfully...";

fin.close();
fout.close();
return 0;
}

Output of program

Enter source file name: file1.txt
Enter target file name: file2.txt
File content copied successfully...

No comments:

Post a Comment