Click here to Skip to main content
12,999,608 members (107,014 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 17 Mar 2009

File Rotator

, 17 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Rotating files by appending 00, 01, 02, 03 etc. in file name once the file size reaches its predefined maximum file size.


Generally if any application keeps logging something in a file, after some time, the log file size becomes very large. To view or search for something in these large log files is difficult. Most of the time, to avoid large log file size I use a file rotation strategy, i.e. once file size reaches a predefined size limit, close the current log file and start logging in a new file. This keeps the size of log files maintainable.

FileRotator Class

FileRotator is a class that implements the file name rotation logic by appending 0, 1, 2, 3, etc. once the file size reaches its predefined size. In the constructor of this class, we can specify the base filename, maximum file size and maximum file count. This class can be used easily with logger or stream data writer. FileRotator creates a file with base filename provided in the constructor. Once this file reaches its maximum size, it renames the current file to myfile00.txt and creates a new file myfile.txt. Again when the new myfile.txt reaches its maximum size, it again renames the current file to myfile01.txt and creates a new file myfile.txt. This process continues till it reaches the maximum file count defined in the constructor. After this, it starts deleting old files myfile00.txt, myfile01.txt, myfile02.txt, etc. It always writes in myfile.txt. This class internally decides if it needs to append 1 or 01 or 001 etc. based on the maximum file count.

Currently old files deletion logic is file name sequence base, later the same can be improved to file creation time base. I also uses similar logic to save the data coming from a stream. I will try to make this class more generic in future.

Using the Code

Using FileRotator class in your existing application is very simple. You just need to add FileRotator.cpp in your project and include FileRotator.h where you want to use this class.

Add a member variable of FileRotator in your existing logger class. Open and close your file using Open() and Close() method of this class. To write something in your file, use Write() method of this class. In the constructor of this class, specify the filename, maximum file size in bytes on which you want to rotate the file and the maximum file count. Default maximum file count is 99. For saving stream base data, you need to make some changes in the code. May be, I can make it more generic in my next version. Here is a sample Logger class:

#include "FileRotator.h"

class MyLogger
    MyLogger() : mFileRotator(1024 /*bytes*/, "myfile.txt")


    int Log(char *str) 
        // customize your log message like adding date, time, etc. here
        string logStr = string("Info:") + str + string("\n");
        return mFileRotator.Write(logStr.c_str());

    FileRotator mFileRotator;

Using the above Logger class:

int _tmain(int argc, _TCHAR* argv[])
    MyLogger myLogger;
    int i = 0;

    while (i < 10000) {
        myLogger.Log("Sample log text");
    return 0;


  • 17th March, 2009: Initial revision


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manish K. Agarwal
Software Developer (Senior) Oracle
India India
Working with Oracle. Using C/C++, VC++, MFC, STL, C#, Java etc. on various platform like Windows, Unix, Macintosh etc. from last 13+ years to convert various type of requirements into running software components. My core expertise is multithreaded desktop product and large scale enterprises software development.

You may also be interested in...

Comments and Discussions

QuestionGood idea. But how about making it an IOStreams stream buffer? Pin
wtwhite24-Mar-09 4:24
memberwtwhite24-Mar-09 4:24 
AnswerRe: Good idea. But how about making it an IOStreams stream buffer? Pin
Manish K. Agarwal24-Mar-09 18:18
memberManish K. Agarwal24-Mar-09 18:18 
GeneralGood Article. Pin
TejaSingh18-Mar-09 19:42
memberTejaSingh18-Mar-09 19:42 
GeneralGood Pin
Onskee117-Mar-09 7:30
memberOnskee117-Mar-09 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170624.1 | Last Updated 17 Mar 2009
Article Copyright 2009 by Manish K. Agarwal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid