Makes a copy of a file. - C++ File Stream

C++ examples for File Stream:File Operation

Description

Makes a copy of a file.

Demo Code

#include <fstream>
#include <iostream>
using namespace std;
#include <stdlib.h>
ifstream in_fp;//from  ww w  .  ja  v  a 2 s  .c  om
ofstream out_fp;
void main()
{
   char in_filename[12];   // Holds original filename.
   char out_filename[12];  // Holds backup filename.
   char in_char;           // Input character.
   cout << "What is the name of the file you want to back up? ";
   cin >> in_filename;
   cout << "What is the name of the file ";
   cout << "you want to copy " << in_filename << " to? ";
   cin >> out_filename;
   in_fp.open(in_filename, ios::in);
   if (!in_fp)
   {
      cout << "\n\n*** " << in_filename << " does not exist ***\n";
      exit(0);   // Exit program
   }
   out_fp.open(out_filename, ios::out);
   if (!out_fp)
   {
      cout << "\n\n*** Error opening " << in_filename << " ***\n";
      exit(0);    // Exit program
   }
   cout << "\nCopying...\n";   // Waiting message.
   while (in_fp.get(in_char))
   {
      out_fp.put(in_char);
   }
   cout << "\nThe file is copied.\n";
   in_fp.close();
   out_fp.close();
   return;
}

Result


Related Tutorials