CJCoding With Joseph
15free / daySign inCreate account

Numbered Append

Write a function void 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 becomes 1. msg, the next 2. msg, and so on (a period and a single space between the number and the message).

Open the file in ios::in mode and count its lines with getline (a missing file counts as 0 lines); then open it in ios::out | ios::app mode and write the new line followed by a newline.

The test starts with an empty file, calls appendNumbered("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
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.