Hello, I have to write a program that merges 2 seperate files and then outputs to a single file. Each input file is to have numbers in ascending order and the output is supposed to compare these and then put them in ascending order.

ex.: input1 = 1 3 5 7
input2 = 2 4 6 8
output = 1 2 3 4 5 6 7 8

I am supposed to create a function with 3 arguments (2 inputs 1 output).

Currently i am at a loss as to where to start and how to compare the 2 files.

I am currently just seeking for some help in getting started, and any advice would be wonderful.

(Sorry if this topic has arrisen before, I searched and was unable to find anything similar.)

For this I would make 3 int vectors: input1, input2, output.

Make a function that reads in a file name and saves it to the vector. void fileToVector(string fileName, vector<int> &in); You would call this twice (once for each file) and then call a function that sorts them into one vector. void mergVectors(vector<int> v1, vector<int> v2, vector<int> &out); Try this out and see where you get with this I would post the whole code but you didn't post any code showing where you are at.

Edited 7 Years Ago by sfuo: n/a

Cool thanks, i'll toy around with that and see where i can get

You're simply merging two files that are ALREADY in order into one output file. Hence only very minimal storage is required (one value from each input file), so vectors are overkill. Have two ifstreams, one ofstream, and two variables of whatever type the file contents are (i.e. int, float, double), one for each of the two input files. Let's call your ofstream outs, and your two ifstreams ins1 and ins2. Let's say the files contains integers. Have two integer variables canlled number1 and number2. ins1 reads into variable number1, ins2 reads into variable number2.

Read values into number1 and number2 from the two files. Compare them. Output the smaller value to outs. Read a new value from whichever file the smaller value came from. Compare them again. Output the smaller value, read in a new one. Keep going, always testing whether either file is empty. If it is, output the remainder of the other file to outs.

This is basically the "merge" part of the Merge Sort.

Edited 7 Years Ago by VernonDozier: n/a

void numbers(ifstream input1, ifstream input2, ofstream output)
{
    int number1, number2;

    input1.open("numbers1.txt");
    input1 >> number1;
    input2.open("numbers2.txt");
    input2 >> number2;
    output.open("output.txt");
    output << number1 << number2;
}

int main()
{   
   return 0;
}

ok so this is what i have so far. I haven't worked on the comparing aspect yet and just need to enter a loop.. This code worked for me without the use of a function, but the function is required. I am having issues with the main() function now. I know that i have to put numbers() in the main function, but i am unsure of how to fulfull the arguments.

The closest i've gotten was something like:

int main()
{   
   ifstream input1;
   ifstream input2;
   ofstream output;
  
   numbers(input1, input2, output);
   return 0;
}

but it gave me an error of not being able to read from a private file or something similar.

I think that this is pretty much the last thing that i need to figure out that i'll have trouble with, the rest should just be filling in some basic things. Any help on this issue would be great, thanks.

#include <string>
#include <fstream>
#include <iterator>
int main() {
   std::ifstream in1("file1.txt");
   std::ifstream in2("file2.txt");
   std::ofstream out("file3.txt");
   std::istream_iterator<std::string> it1(in1);
   std::istream_iterator<std::string> it2(in2);
   std::istream_iterator<std::string> end;

   while ( it1 != end && it2 != end ) {
      out << *it1++ << *it2++;
   }

   return 0;
}
Comments
Giving away code.
good

I am new here >> I want 2 know this forum for which country?

sorry for distrupution

The problem is i have to use a function to run all of this and i am unsure as to how to implement that under int main(). And also how do i test whether or not the file is empty? Thanks again

Edited 7 Years Ago by Kaotic07: n/a

void merge(const std::string& fn1, const std::string& fn2, const std::string& ofn) {
   std::ifstream in1(fn1);
   std::ifstream in2(fn2);
   std::ofstream out(ofn);
   std::istream_iterator<std::string> it1(in1);
   std::istream_iterator<std::string> it2(in2);
   std::istream_iterator<std::string> end;

   while ( it1 != end && it2 != end ) {
      out << *it1++ << *it2++;
   }
}

I understand that part of it, i just can't figure out how to cause the function to initiate via int main(). I know i have to put numbers(argument, argument, argument) but i do not know how to make those arguments be fulfilled.

int main() {
   std::string fn1("");
   std::string fn2("");
   std::string out("");

   merge(fn1, fn2, out);

   return 0;
}

This solution has one condition. Both the original file must be the same size, otherwise copying will occur until the reach the end of the smallest file.

PS
Since the function requires arguments of type "const std::string &", then it can pass arguments to the type "const char *". In this case, in the stack will be created temporary object of type "const std::string", which destructed when you exit the function body.

ex.:

merge("file1.txt", "file2.txt", "outfile.txt");

Edited 7 Years Ago by niXman: n/a

If you can get it to work without a function, it's a matter of converting that code so that it's inside of a function. Post the code that works where it's all in main.

This question has already been answered. Start a new discussion instead.