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++.
Want to practice Programming Questions, Download more than 400+ programming question to improve your coding skills.
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.
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.
I hope you like my program please share it on social media.
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
SOURCE.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
- Write a Program to Open the File and Read the Content to Find the Sum of All Values in the File
- C++ Program to Count 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