Merging 2 Files

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 10
Reputation: Kaotic07 is an unknown quantity at this point 
Solved Threads: 0
Kaotic07 Kaotic07 is offline Offline
Newbie Poster

Merging 2 Files

 
0
  #1
28 Days Ago
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.)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
0
  #2
28 Days Ago
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.
Last edited by sfuo; 28 Days Ago at 5:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Kaotic07 is an unknown quantity at this point 
Solved Threads: 0
Kaotic07 Kaotic07 is offline Offline
Newbie Poster
 
0
  #3
27 Days Ago
Cool thanks, i'll toy around with that and see where i can get
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #4
27 Days Ago
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.
Last edited by VernonDozier; 27 Days Ago at 2:09 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Kaotic07 is an unknown quantity at this point 
Solved Threads: 0
Kaotic07 Kaotic07 is offline Offline
Newbie Poster
 
0
  #5
26 Days Ago
  1. void numbers(ifstream input1, ifstream input2, ofstream output)
  2. {
  3. int number1, number2;
  4.  
  5. input1.open("numbers1.txt");
  6. input1 >> number1;
  7. input2.open("numbers2.txt");
  8. input2 >> number2;
  9. output.open("output.txt");
  10. output << number1 << number2;
  11. }
  12.  
  13. int main()
  14. {
  15. return 0;
  16. }

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:
  1. int main()
  2. {
  3. ifstream input1;
  4. ifstream input2;
  5. ofstream output;
  6.  
  7. numbers(input1, input2, output);
  8. return 0;
  9. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: niXman is an unknown quantity at this point 
Solved Threads: 10
niXman's Avatar
niXman niXman is offline Offline
Light Poster
 
0
  #6
26 Days Ago
  1. #include <string>
  2. #include <fstream>
  3. #include <iterator>
  4. int main() {
  5. std::ifstream in1("file1.txt");
  6. std::ifstream in2("file2.txt");
  7. std::ofstream out("file3.txt");
  8. std::istream_iterator<std::string> it1(in1);
  9. std::istream_iterator<std::string> it2(in2);
  10. std::istream_iterator<std::string> end;
  11.  
  12. while ( it1 != end && it2 != end ) {
  13. out << *it1++ << *it2++;
  14. }
  15.  
  16. return 0;
  17. }
Я из Молдавии. Говорю на Русском.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: it-lady is an unknown quantity at this point 
Solved Threads: 1
it-lady it-lady is offline Offline
Newbie Poster
 
0
  #7
26 Days Ago
I am new here >> I want 2 know this forum for which country?

sorry for distrupution
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Kaotic07 is an unknown quantity at this point 
Solved Threads: 0
Kaotic07 Kaotic07 is offline Offline
Newbie Poster
 
0
  #8
26 Days Ago
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
Last edited by Kaotic07; 26 Days Ago at 2:12 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: niXman is an unknown quantity at this point 
Solved Threads: 10
niXman's Avatar
niXman niXman is offline Offline
Light Poster
 
0
  #9
26 Days Ago
  1. void merge(const std::string& fn1, const std::string& fn2, const std::string& ofn) {
  2. std::ifstream in1(fn1);
  3. std::ifstream in2(fn2);
  4. std::ofstream out(ofn);
  5. std::istream_iterator<std::string> it1(in1);
  6. std::istream_iterator<std::string> it2(in2);
  7. std::istream_iterator<std::string> end;
  8.  
  9. while ( it1 != end && it2 != end ) {
  10. out << *it1++ << *it2++;
  11. }
  12. }
Я из Молдавии. Говорю на Русском.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Kaotic07 is an unknown quantity at this point 
Solved Threads: 0
Kaotic07 Kaotic07 is offline Offline
Newbie Poster
 
0
  #10
26 Days Ago
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC