19/03/2023

C++ Program to Copy The Contents of One File to Another

Write a C++ Program to Copy The Contents of One File to Another. Write a program to copy the contents of abc.txt to xyz.txt. C++ program to copy one file to another. File Handling Is Used. In this program, there are two files and we have to copy the first file's data to another file. Keep in mind while doing this either keep both files located in the same folder or you can provide a path of the source file and destination file. In the first step, you have to create two files one is abc.txt and the second one is xyz.txt then after putting some content in the source file and save the file.

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.

C++ Program to Copy The Contents of One File to Another

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


The Output of C++ Program to Copy One File to Another

ABC.TXT


ABC.TXT

XYZ.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

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

1 comment:

  1. C++https://www.programmingwithbasics.com/2016/03/write-c-program-for-copy-one-file-to.html?m=1

    ReplyDelete