Write a C++ Program to Copy one file to another file using File Handling

By | 05.01.2017

C++ Program to Copy one file to another file using File Handling

 

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
        ifstream fs;
        ofstream ft;
        char ch, fname1[20], fname2[20];
        cout<<"Enter source file name with extension (like files.txt) : ";
        cin>>fname1;
        fs.open(fname1);
        if(!fs)
        {
                cout<<"Error in opening source file..!!";
                
                exit(1);
        }
        cout<<"Enter target file name with extension (like filet.txt) : ";
        cin>>fname2;
        ft.open(fname2);
        if(!ft)
        {
                cout<<"Error in opening target file..!!";
                fs.close();
        
                exit(2);
        }
        while(fs.eof()==0)
        {
                fs>>ch;
                ft<<ch;
        }
        cout<<"File copied successfully..!!";
        fs.close();
        ft.close();
        return 0;
}

 

OUTPUT ::

 

Enter source file name with extension (like files.txt) : test.txt
Enter target file name with extension (like filet.txt) : test1.txt
File copied successfully..!!

 

5 1 vote
Article Rating
Category: C++ Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments