-1
 #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.

  • `file1>>num1` etc can fail at the end of file, so loop with `while(file1>>num1)`, etc. This will prevent the last integer being duplicated. Next, your sorting routine needs nested loops, not just a single loop. Finally, you do not need the intermediate `file3`. – Ken Y-N Nov 27 '17 at 05:54
  • @KenY-N what is the difference if I use nested loops? I tried using a single loop for a non-file related program and it worked. – lakshay.angrish Nov 27 '17 at 06:07
  • For basic sorting algorithms like Insertion Sort, Bubble Sort, etc, you need nested loops. You cannot sort in a single pass. – Ken Y-N Nov 27 '17 at 06:10
  • @KenY-N I am a beginner. Could you help me with the correct code? – lakshay.angrish Nov 27 '17 at 06:11
  • @KenY-N And, could you explain how does file>>num1 fail at the end of the file? – lakshay.angrish Nov 27 '17 at 06:16
  • Writing a sort is a suitable task for learners; please try doing it yourself. Regarding `while(file>>num1)` versus `while(file)`, [this answer discusses the issues](https://stackoverflow.com/a/5605159/1270789). – Ken Y-N Nov 27 '17 at 06:30
  • Avoid using passive voice in technical documentation. The phrase "they should be sorted" could mean "The input files will be pre-sorted," or it could mean, "The program shall sort the combined contents of the files." – Jive Dadson Nov 27 '17 at 11:14
  • 1
    You need to learn to divide your problems into smaller problems. If your question is about sorting, none of the file IO stuff is relevant, so most of your code could be removed. If your question is about file IO, then all the sorting stuff can be removed. In either case, you need to provide clear inputs and expected outputs, and a clear problem statement. "I cannot sort them using the above code" doesn't tell us anything about what is wrong with the code you've posted or how its behavior differs from your expectations. – meager Nov 27 '17 at 14:41
  • @meagar yeah, I get that, but I actually did not know what could be wrong, the file io or sorting. What should I do in that situation? – lakshay.angrish Nov 27 '17 at 14:43

2 Answers2

2

If you possible, you normally want to use the algorithms built into the standard library, so (for example) to do the sorting, you could use std::sort.

std::ifstream in1("source1.txt");
std::ifstream in2("source2.txt");

std::vector<int> numbers;

std::copy(std::istream_iterator<int>(in1), {}, std::back_inserter(numbers));
std::copy(std::istream_iterator<int>(in2), {}, std::back_inserter(numbers));

std::sort(numbers.begin(), numbers.end());

std::ofstream out("target.txt");

std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<int>(out, "\n"));

If the numbers in each input file are sorted, so you just need to merge two sorted streams, you could use std::merge instead:

std::ifstream in1("source1.txt");
std::ifstream in2("source2.txt");
std::ofstream out("target.txt");

std::merge(std::istream_iterator<int>(in1), {},
           std::istream_iterator<int>(in2), {},
           std::ostream_iterator<int>(out, "\n"));
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
0

You can try alternately reading from either file depending on which had the smaller number previously:

file1 >> num1;
file2 >> num2;
while(file1 || file2)
{
   if (!file2)
   {
     file3 << num1 << "\n";
     file1 >> num1;
   }
   else if (!file1)
   {
     file3 << num2 << "\n";
     file2 >> num2;
   }
   else if (num1 < num2)
   {
     file3 << num1 << "\n";
     file1 >> num1;
   }
   else
   {
     file3 << num2 << "\n";
     file2 >> num2;
   }

}