| | |
Merging 2 Files
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
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.)
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.)
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.
You would call this twice (once for each file) and then call a function that sorts them into one vector.
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
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.
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.
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#5 26 Days Ago
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
0
#6 26 Days Ago
C++ Syntax (Toggle Plain Text)
#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; }
Я из Молдавии. Говорю на Русском.
0
#9 26 Days Ago
C++ Syntax (Toggle Plain Text)
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++; } }
Я из Молдавии. Говорю на Русском.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Structures
- Next Thread: Explanation of code line by line
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






