[英]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. 在Visual Studio 2008中使用C ++ MFC,我尝试使用CFile或CStdioFile读取文本文档的最后一行,存储它,然后在文件修改了文本之后重新打印它。
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. 我已经知道该部分可以工作了,唯一的问题是它不是动态的,您必须手动创建一个offSet,无论最后一行有多长。 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. 我希望它读取</ Station>,直到到达<,一路计数。
将TCHAR lastTag [1]更改为char lastTag [1]已解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.