GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Feb-2009, 02:05
unbear unbear is offline
New Member
 
Join Date: Feb 2009
Posts: 1
unbear is on a distinguished road

Reading a log file which is currently writen by another application


Hello,

I am trying to read a log file which is written by another application.

Other application is running,and writing lines to log file time to time.
At the same time I supposed to read log file without lose and as soon as modification occurs.


Even I check if the error occurs when opening file,
I can not get the line written when open file error occurs.
But it can not get some lines of the log file.

Below you can find the function which I am using to accomplish this job.


Thanks a lot

p.s. : I can not modify the other application.

CPP / C++ / C Code:
while(1) 
        { 
            loopTry++; 
            if(CFile::GetStatus(fileName,filestatus)) 
            { 
                if(filestatus.m_size > lastSize) 
                { 
                    if ( !myFile.Open( fileName, CFile::shareDenyWrite | //  
                    CFile::modeRead  , &fileException ) )  
                    { 
                        CString cs; cs.Format(_T("Can't open file %s, error = %u readTry = %d"), 
                        fileName, fileException.m_cause,readTry ); 
                        log(cs); 
 
                    } 
                    else//oku 
                    { 
                        readTry++; 
                        char * rBuffer = ::new char[filestatus.m_size+1-cursorPos]; 
                        rBuffer[filestatus.m_size-cursorPos] = '\0'; 
 
                        //READ START 
                        myFile.LockRange(0,filestatus.m_size);// 
                        myFile.Seek(cursorPos, CFile::begin);//TODO: compare seek overhead with reading up to cursorPos 
                        myFile.Read(rBuffer,filestatus.m_size-cursorPos); 
                        myFile.UnlockRange(0,filestatus.m_size); 
                        myFile.Close(); 
                        //READ END 
                        lastSize = filestatus.m_size; 
                        //PROCESS START 
                        std::string str(rBuffer); 
                        int lineEndPos = 0; 
                        int localCursor = 0; 
                        for(;;) 
                        { 
                        std::string line; 
                         
 
                        int lastNewlineIndex = str.find("\r\n",lineEndPos+1); 
                        if(lastNewlineIndex == std::string::npos)//if there is no newline left 
                        { 
                        break; 
                        } 
                        lineEndPos = lastNewlineIndex; 
                        line = str.substr(localCursor,lineEndPos-localCursor); 
                        ProcessLine(line,loopTry,readTry);// some func to process line read  
                        localCursor = lineEndPos + 2; 
                        cursorPos += localCursor; 
                        } 
                        //PROCESS END 
                        ::delete [] rBuffer; 
                    } 
                } 
                else//file size is not changed. wait                { 
                Sleep(wait); 
                } 
            } 
            else 
            { 
                CString cs; cs.Format(_T("can not get status. looptry = %d"),loopTry ); 
                log(cs); 
            }    
        }
Last edited by LuciWiz : 23-Feb-2009 at 02:08. Reason: Please insert your C++ between [cpp] & ]/cpp] tags
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
contents of .txt file into 2D array anirudhroxrulz C Programming Language 4 10-Apr-2008 22:45
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
How to use the data obtain in one C file to another C file? TommyC C Programming Language 12 15-Jan-2008 09:51
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 20:03
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 03:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:14.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.