← Back to Streams
Question 669
Merge Two Files Uniquely
Merge Two Files Uniquely
Write a functionint mergeUnique(const string& a, const string& b, const string& out)that reads two text files and writes a third one containing every distinct line from either input, sorted in ascending (dictionary) order, one per line, each followed by a newline. Duplicates, whether within one file or across both, appear only once. Return the number of lines written. Both input files contain one word per line with no blank lines. Read each input with anfstreaminios::inmode using agetlineloop, collect the lines in aset<string>, then openoutinios::out | ios::truncmode and write the set. The test creates the two input files, prints the returned count on its own line, then prints every line of the output file. For file a containingpear,appleand file b containingkiwi,appleit prints the output shown in Expected Output. Do NOT write a main function.
Expected Output:
3 apple kiwi pear 3 ant cat dog
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Streamsfstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.