3/13/2016

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

Write a Program to Copy Contents of One File to Another in C++. A Very Simple Program to Copy One File to Another in C++. File Handling Is Used. In this program, there are two files and we have to copy the first file 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. Explanation of Write a Program to Copy One Text File to Another C++. The first step you have to create a two file one is source.txt and second one is destination.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 source.txt. now press enter. Our Program to Copy One Text File to Another C++ done rest of the things and copied the source.txt file content to the destination.txt that we have created early. If anything goes wrong then program displayed the errors name.
Simple Program to Copy Contents of One File to Another in C++.

Write a Program to Copy Contents of One File to Another in Cpp

Want to practice Programming Questions, Download more than 400+ programming question to improve your coding skills.

Explanation of Program to Copy Contents of One File to Another in C++


#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 in this Program to Copy Contents of One File to Another in C++ in C Programming Language. We have copied the one file into another file. The program first will ask you to enter the first file name after that ask for entering the second file name than program copied the first file into the second file.

We have to enter both file name with extension cause file always have extension or folder not this is useful when you want to merge two files or in other words, we can say that merge two file.

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


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

SOURCE.TXT


SOURCE.TXT

DESTINATION.TXT


DESTINATION.TXT


Write a C++ Program That Copy Contents of One File to Another File is complete successfully. Now it's time to check another file destination.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