← Back to Streams
Question 661
Numbered Append
Numbered Append
Write a functionvoid appendNumbered(const string& filename, const string& msg)that appends a numbered line: the number is one more than the number of lines the file already contains, so the first message becomes1. msg, the next2. msg, and so on (a period and a single space between the number and the message). Open the file inios::inmode and count its lines withgetline(a missing file counts as 0 lines); then open it inios::out | ios::appmode and write the new line followed by a newline. The test starts with an empty file, callsappendNumbered("log_a.txt", "boot"), then the same with"run", then"stop", and prints every line of the file (see Expected Output). Do NOT write a main function.
Expected Output:
1. boot 2. run 3. stop 1. init 2. ready
= 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.