Now Run the program using Dev C++ and when the program requests you to enter the name of the source file, then enter the abc.txt. now press enter. Our Program to Copy One Text File to Another in C++ did the rest of the things and copied the abc.txt file content to the xyz.txt that we created early.
If anything goes wrong then the program displayed the errors name. Simple Program to Copy Contents of One File to Another in C++.
C++ Program to Copy The Contents of One File to Another
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fs;
ofstream ft;
string str;
char sourcefile[50], destinationfile[50];
cout << "Enter Source File with Extension: ";
gets(sourcefile);
fs.open(sourcefile);
if (!fs)
{
cout << "Error in Opening Source File...!!!";
exit(1);
}
cout << "Enter Destination File with Extension: ";
gets(destinationfile);
ft.open(destinationfile);
if (!ft)
{
cout << "Error in Opening Destination File...!!!";
fs.close();
exit(2);
}
if (fs && ft)
{
while (getline(fs, str))
{
ft << str << "\n";
}
cout << "\n\n Source File Date Successfully Copied to Destination File...!!!";
}
else
{
cout << "File Cannot Open...!!!";
}
cout << "\n\n Open Destination File and Check!!!\n";
fs.close();
ft.close();
}
So this Program Copy Contents of One File to Another in C++ in C Programming Language. We have copied one file into another file. The program first will ask you to enter the first file name after that ask you for entering the second file name then the program copied the first file into the second file.
We have to enter both file names with an extension cause files always have an extension or folder not this is useful when you want to merge two files, in other words, we can say merge two files.
The Output of C++ Program to Copy The Contents of One File to Another
ABC.TXT
XYZ.TXT
Write a C++ Program That Copies the Contents of One File to Another File is complete successfully. Now it's time to check another file xyz.txt.
Similar C++ File Handling
- C++ Program For Find The Sum of Integers In a File
- C++ Program to Count the Number of Characters in a File
- C++ Program to List All Files in a Directory and Sub Directories
- Write a C++ Program to Merge Two Files into One File
- C++ Program to Read and Write a Text File Line by Line
I hope you like my program please share it on social media.
C++https://www.programmingwithbasics.com/2016/03/write-c-program-for-copy-one-file-to.html?m=1
ReplyDelete