Search code examples
c++mfccfile

CStdioFile::GetPosition on long file (15 MB) is wrong by few bytes


I have following code where the idea is to read a text file line by line and save the current position m_numBytesRead. So if I break the loop (on my case to split text parsing by chunks on big files) and I try to access a second time by making a Seek of m_numBytesRead-1, the ReadString is not geting the begin of the line as I expected.

CStdioFile fileLog;
if (fileLog.Open(m_strReadFileName, CFile::modeNoTruncate | CFile::modeRead | CFile::shareDenyNone))
{
    if (m_numBytesRead > 0)
        fileLog.CStdioFile::Seek(m_numBytesRead-1, CFile::begin);

    bool bBreakLoop = false;
    while (fileLog.ReadString(strLine) && !bBreakLoop)
    {

        // any condition to set bBreakLoop after few MB read...

        if (!bBreakLoop)
        {
            m_numBytesRead = fileLog.CStdioFile::GetPosition();
        }
    };
    fileLog.Close();
}

By debuging more in detail and comparing with the indexes I get on Notepad++, it seems that the CStdioFile::GetPosition() is not giving correct value, begining of new line to be read, but few bytes (12 on my case) more...

Is is a bug on MFC or is there something I'm missing there ? Does someone see similar issues ?

Note that I'm using VS2010 on Windows 7.


Solution

  • Add open mode CFile::typeBinary to get byte-exact offsets. The default mode is text, which performs newline conversion which may mess up offsets.