← Back to Streams
Question 618
Rotating Logger
Rotating Logger
Create a classLoggerthat writes log lines to a file but never lets the file grow beyond a limit. 1. ConstructorLogger(const string& filename, int maxLines): stores both values. It does not create or touch the file. 2.int lineCount() const: opens the file with anifstreamand returns how many lines it currently contains (count withgetline). If the file does not exist or cannot be opened, return 0. 3.void log(const string& msg): if the file already containsmaxLineslines or more, open it in truncating mode; otherwise open it in append mode. Then writemsgfollowed by a newline and close the file. Print nothing tocout. Test 1 removes the file, createsLogger lg("rot_a.txt", 2), callslg.log("a")andlg.log("b"), printslg.lineCount(), then callslg.log("c")and printslg.lineCount()again, then reopens the file and prints every line. The output is exactly what is shown in Expected Output. Do NOT write a main function.
Expected Output:
2 1 c 3 2 s t
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Streamsofstream
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.