← Back to Streams
Question 657
Append Then Count Lines
Append Then Count Lines
Write a functionint appendAndCount(const string& filename, const string& line)that: 1. opensfilenamewith anfstreaminios::out | ios::appmode and writeslinefollowed by a newline at the end of the file; 2. closes the stream and reopens the samefstreamobject on the same file inios::inmode; 3. counts the lines in the file with agetlineloop and returns that count. The test writes the two linesoneandtwointo the file first, callsappendAndCount("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
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.