Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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();
}

 

Output:

Enter source file with extension: source.txt

Enter destination file with extension : destination.txt

source file date successfully copied to destination file !!

open destination file and check !!!

 

Source.txt

 

Destination.txt

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to Merge Two Files Into Third File Usi... >>
<< C++ Program to List and Display Files in Current D...