Thread: File handling question...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    File handling question...

    I have a class that I am using to Log Debug Messages in ".log" files.

    The problem I am having is getting the class to only save 30 log files.

    So if I have file: Log_1.log, etc, etc, Log_30.log then I want the program to delete Log_1.log and save the new Debug log as Log_31.log. That way the names don't lost meaning.

    I have tried, and tried, and tried. It must be something simple that I am overlooking.
    I have the following code. It will only save 10 files, and I have absolutely no idea what it is doing. Any assistance would be greatly appreciated.

    Also, as far as the code that finds the files. I am 100% sure that it works the way it is suppose to.
    Code:
    MT_Log::MT_Log()
    {
        //Gather all files, and store them in filelist
        flist list;
        BuildFileList(list);
    
        int FirstFileID = 0;
        int LastFileID = 0;
    
        int FileID = 0;
        //Get first filename from list
        if ( list.files != 0 )
        {
            std::stringstream NameParser;
            std::string FileName;
            //Get FileID from filename
            FileName = list.files[0].cFileName;
            NameParser << FileName.substr(FileName.find("Log_")+4, FileName.find(".log"));
            NameParser >> FileID;
        }
        
        FirstFileID = FileID;
    
        FileID = 0;
        //Get last filename from list
        if ( list.files != 0 )
        {
            std::stringstream NameParser;
            std::string FileName;
            //Get FileID from filename
            FileName = list.files[list.num_entries-1].cFileName;
            NameParser << FileName.substr(FileName.find("Log_")+4, FileName.find(".log"));
            NameParser >> FileID;
        }
        LastFileID = FileID;
    
        if ( ( FirstFileID+30 ) < LastFileID )
        {
            std::string File = "Logs\\";
            File += list.files[0].cFileName;
            std::remove(File.c_str());
        }
    
        std::stringstream ss;
        ss << "Logs\\Log_" << LastFileID+1 << ".log";
    
        log.open(ss.str().c_str(),std::ios::trunc);
    
        // free the list of files
        free(list.files);
    }
    Thank you SO much if you can help.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when you creating file 31
    LastFileID == 30
    file to remove is FirstFileID == 1

    Code:
    if ( ( FirstFileID+30 ) < LastFileID )
    Could you see something wrong in this expression?

    PS. free(list.files); - shouldn't it be done by the destructor of th eobject?
    Shouldn't you use new/delete pair in C++?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Lol, still not catching the expression problem, but

    free(list.files); frees the local list in the constructor.

    I am rewriting some things. I was using realloc(). Which I decided against, because it is a pain in the butt.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if ( ( FirstFileID+30 ) < LastFileID )
    I think you turned that logic backwards.
    What you wanted is
    If CurrentFileId is greater than the maximum file Id
    Or, in other words, something like...
    Code:
    if ( LastFileId >= (FirstFileId + 30) )
    Last edited by Elysia; 04-10-2008 at 01:36 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Found my solution. Pretty much rewrote the whole thing, but here is the solution if anyone ever needs it. ( doubtful )
    Code:
    struct flist
    {
       std::vector<WIN32_FIND_DATAA> files;
       std::vector<WIN32_FIND_DATAA>::iterator iter;
    };
    
    {
            flist list;
    	BuildFileList(list);
    	std::vector<int> IDList;
    	
    	//Populate the IDList vector
    	for ( list.iter = list.files.begin(); list.iter != list.files.end(); ++ list.iter )
    	{
    		int FileID = -1;
    		std::stringstream Parser;
    		Parser << (*list.iter).substr((*list.iter).find("Log_")+4, (*list.iter).find(".log"));
    		Parser >> FileID;
    		IDList.push_back(FileID);
    	}
    	
    	//Sort the IDList vector
    	std::sort(IDList.begin(), IDList.end());
    
    	//FileID variables
    	int LowFileID = 0;
    	int HighFileID = -1;
    
    	//Get variables
    	if ( IDList.size() != 0 )
    	{
    		LowFileID = IDList.front();
    		HighFileID = IDList.back();
    	}
    	if ( ( LowFileID+29 ) < HighFileID )
    	{
    		std::stringstream File;
    		File << "Logs\\Log_" << LowFileID << ".log";
    		std::remove(File.str().c_str());
    	}
    
    	std::stringstream LogFile;
    	LogFile << "Logs\\Log_" << HighFileID+1 << ".log";
    	
    	log.open(LogFile.str().c_str(), std::ios::trunc);
    }
    QUESTION:
    Does anyone think that since I am using Win32 that it would make more sense to delete the files by date created, so that tampering won't mess with the order?
    Last edited by Raigne; 04-10-2008 at 01:36 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That logic is still turned backwards, though
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    That logic is still turned backwards, though
    seems to be ok if diff between High and Low is less than 29 - there is still not enogh files to start removing
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure it works, but it's backwards in that I think it would be easier to understand if you compared it to current file id is bigger than the maximum allowed instead of the maximum id is lesser than the current.
    The second making a little less sense than the first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Mutlithreaded file handling
    By nvoigt in forum Windows Programming
    Replies: 11
    Last Post: 06-30-2005, 02:39 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM