CJCoding With Joseph
15free / daySign inCreate account

Merge Two Files Uniquely

Write a function int 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 an fstream in ios::in mode using a getline loop, collect the lines in a set<string>, then open out in ios::out | ios::trunc mode 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 containing pear, apple and file b containing kiwi, apple it 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
Topics:
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.