#include<iostream>
 #include<fstream>
 using namespace std;
 int main()
 {
 fstream file1,file2,file3,file4;
 int num1,num2,num,temp=0,a[20];
 file1.open("source1.txt",ios::in);
 file2.open("source2.txt",ios::in);
 file3.open("sample.txt",ios::out|ios::in|ios::trunc);
 file4.open("target.txt",ios::out|ios::in|ios::trunc);
 if(file1.is_open()&&file2.is_open()&&file3.is_open()&&file4.is_open())
 {
 while(file1)
 {
  file1>>num1;
  file3<<num1<<'\n';
 }
 file1.close();
 while(file2)
  {
    file2>>num2;
    file3<<num2<<'\n';
  }
  file2.close();
  file3.seekg(0);
  int i=0;
  while(file3)
  {
    file3>>num;
    a[i]=num;
      i++;
  }
  for(int j=0;j<i-1;j++)
  {
    if(a[j]<a[j+1])
    {
      temp=a[j+1];
      a[j+1]=a[j];
      a[j]=temp;
    }
  }
  file3.close();
  for(int j=0;j<i;j++)
  file4<<a[j]<<'\n';
  file4.close();
  }
  else
  cerr<<"\nError!";
  return 0;
  }
source1 and source 2 contains integers which have to be copied to target and they should be sorted. I cannot sort them using the above code. Also, I see that the last integer from both source1 and source2 is copied twice.
 
     
    