thespeedracer38

File2

Feb 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     char file1[20], file2[20], file3[20], ch;
  8.     long i, n, limit;
  9.     system("cls");
  10.    
  11.     // Taking the name of the files as input
  12.    
  13.     cout << "Enter the input file name: ";
  14.     cin >> file1;
  15.     cout << "Enter the output file 1 name: ";
  16.     cin >> file2;
  17.     cout << "Enter the output file 2 name: ";
  18.     cin >> file3;
  19.    
  20.     ifstream fp1;
  21.     ofstream fp2;
  22.     ofstream fp3;
  23.    
  24.     // Opening the file
  25.     fp1.open(file1, ios::binary);
  26.     fp2.open(file2, ios::binary);
  27.     fp3.open(file3, ios::binary);
  28.    
  29.     // Moving the file object to the end of the file
  30.     fp1.seekg(0, ios::end);
  31.    
  32.     // To get the last byte position
  33.     n = fp1.tellg();
  34.     // Calculating limit
  35.     limit = n / 2;
  36.    
  37.     // Moving file object to the begining of the
  38.     // file
  39.     fp1.seekg(0, ios::beg);
  40.    
  41.     // To copy data from the input file to the
  42.     // output file
  43.     for(i = 1; i <=n ; i++){
  44.         ch = fp1.get();
  45.         if(i <= limit){
  46.             fp2.put(ch);
  47.         }
  48.         else {
  49.             fp3.put(ch);
  50.         }
  51.     }
  52.    
  53.     // Closing the files
  54.     fp1.close();
  55.     fp2.close();
  56.     fp3.close();
  57.    
  58.     // Displaying the size of the file in bytes
  59.     cout    << "\nFile copy is over, Number of bytes copied = "
  60.             << n << endl;
  61.     cout    << "Size of <" << file2 << "> = " << limit << " bytes\n";
  62.     cout    << "Size of <" << file3 << "> = " << (n - limit) << " bytes" << endl;
  63.     return 0;
  64. }
Add Comment
Please, Sign In to add comment