Question Reading a log file which is currently writen by another application RRS feed

  • Question

  • 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.

                    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 ); 
                            char * rBuffer = ::new char[filestatus.m_size+1-cursorPos]; 
                            rBuffer[filestatus.m_size-cursorPos] = '\0'; 
                            //READ START 
                            myFile.Seek(cursorPos, CFile::begin);//TODO: compare seek overhead with reading up to cursorPos 
                            //READ END 
                            lastSize = filestatus.m_size; 
                            //PROCESS START 
                            std::string str(rBuffer); 
                            int lineEndPos = 0
                            int localCursor = 0
                            std::string line; 
                            int lastNewlineIndex = str.find("\r\n",lineEndPos+1); 
                            if(lastNewlineIndex == std::string::npos)//if there is no newline left 
                            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                { 
                    CString cs; cs.Format(_T("can not get status. looptry = %d"),loopTry ); 

    Monday, February 23, 2009 8:37 AM

All replies