← Back to Streams
Question 597
First Differing Line
First Differing Line
Write a functionint firstDifference(const string& a, const string& b)that compares two text files line by line and returns the 1-based number of the first line where they differ, or 0 if the files are identical. If one file ends before the other, the first line that exists in only one of them is the first difference. Examples (each test creates both files): - linesx,y,zversusx,Q,zreturns 2 - linesx,y,zversusx,y,zreturns 0 - linesa,bversusa,b,creturns 3 The test prints the returned number on its own line, for example (see Expected Output). Do NOT write a main function.
Expected Output:
2 0 3
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Streamsifstream
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.