0

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.

user3215251
  • 239
  • 1
  • 16

1 Answers1

0

Changing TCHAR lastTag[1] to char lastTag[1] has solved the issue.

user3215251
  • 239
  • 1
  • 16
  • Something is *badly* wrong in that case. The backing type for `CString` should match the type of `TCHAR`. – Cody Gray - on strike May 28 '14 at 13:21
  • When I was stepping through it with break points, the TCHAR lastTag[0] always returned 4 nulls, a character that never changed after the first time it was set, and then another null. – user3215251 May 28 '14 at 13:36
  • It is an array with a length of 1. It can't have 4 nulls unless you read beyond the end of the array. – Cody Gray - on strike May 28 '14 at 13:37
  • That's just what got spit out in Visual Studio's local variables during debugging. It was registering as 4 Squares, a random character, and another Square. The reason I said NULL was just an assumption. – user3215251 May 28 '14 at 13:54
  • You haven't initialized the array yet. It has garbage values. As a result, the call to `Seek` is meaningless. You are asking it to seek a garbage number of bytes from the end. `Seek` is not filling an array. And it returns a value, which you should probably be checking. – Cody Gray - on strike May 28 '14 at 14:00