I have a class that has a filestream of type ofstream. The constructor opens the file in append mode and all the messages always get written at the end of the file.
I need to write into outputFile up to some fixed size say 1Mb, then I need to close, rename, and compress it, and then open a new file of the same name.
This needs to be done when a certain size of file is reached.
I tried using tellg() but after reading stuffs (and this) on internet, I understood that this is not the right approach.
As I'm new to C++, I'm trying to find out the most optimized and correct way to get the accurate current size of file opened by ofstream?
class Logger {
std::ofstream outputFile;
int curr_size;
Logger (const std::string logfile) : outputFile(FILENAME,
std::ios::app)
{
curr_size = 0;
}
};
Somewhere in the program, I'm writing data into it:
// ??? Determine the size of current file ???
if (curr_size >= MAX_FILE_SIZE) {
outputFile.close();
//Code to rename and compress file
// ...
outputFile.open(FILENAME, std::ios::app);
curr_size = 0;
}
outputFile << message << std::endl;
outputFile.flush();