Create a Log class that opens a file, writes messages, and closes the file. The
ID: 3630366 • Letter: C
Question
Create a Log class that opens a file, writes messages, and closes the file.The constructor takes a filename parameter and opens the file.
Supply a method called entry() that takes a string parameter containing the message to be written to the log.
When writing the message to the log, FIRST write a timestamp. Use _strtime_s() to obtain a timestamp.
Keep a count of the number of messages written to the log, and provide a method to return this number.
Close the file in the Log destructor.
Create a main program that instantiates the Log, then writes a few messages to the log. Before the main program exits, display the number of log messages that were written to the file.
Use an editor to open your log file and confirm that the messages were written correctly.
Explanation / Answer
#include #include #include #include using namespace std; class Log { private: string errorMsg; string fileName; ifstream theFile; ofstream fileWrite; int logMessagesCount; public: Log (string fileToOpen) : fileName(fileToOpen) { theFile.open(fileName); logMessagesCount = 0; errorMsg = ""; } void entry(string errMsg) { errorMsg = errorMsg + errMsg + " "; logMessagesCount++; } int numberOfLogMessages() { return logMessagesCount; } void printLog() { char date[9]; _strtime_s(date); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.