简体   繁体   中英

CFile and CStdioFile Reading one byte at a time

Using C++ MFC with Visual Studio 2008, I am trying to using CFile or CStdioFile to read in the last line of a text document, store it, and then reprint it after the file has had text amended to it.

I have gotten that part working, the only problem is is that it is not dynamic, you have to manually created an offSet for however long the last line is. As such, I am trying to make a function that reads the last line until it finds a common element in all of the files this will be working with, and count how many bytes there were. This is what I have now for that:

int MeasureLastTag(CStdioFile* xmlFile)
{
TCHAR lastTag[1];
CString tagBracket = _T("");
xmlFile->Seek(0, CFile::end);
int count = 0;

while(tagBracket != _T("<"))  //Go back two, read ahead one
{
    xmlFile->Seek(-2, CFile::current);
    xmlFile->Read(lastTag, 1);
    tagBracket = lastTag;
    count++;
}

return count;
}

However, this causes an infinite loop that I can't seem to shake. Any ideas on how to make it work?

Additional Information, this is a sample of the file.

<Station>
</Station>

I want it to read < /Station> until it gets to the <, counting along the way.

将TCHAR lastTag [1]更改为char lastTag [1]已解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM