CJCoding With Joseph
15free / daySign inCreate account

Append Then Count Lines

Write a function int appendAndCount(const string& filename, const string& line) that:
1. opens filename with an fstream in ios::out | ios::app mode and writes line followed by a newline at the end of the file;
2. closes the stream and reopens the same fstream object on the same file in ios::in mode;
3. counts the lines in the file with a getline loop and returns that count.

The test writes the two lines one and two into the file first, calls appendAndCount("entries_a.txt", "three"), and prints the returned count on its own line (see Expected Output).

Do NOT write a main function.

Expected Output:

3
1

= 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.