1

I need to count the number of lines in a text file. This is the code I have right now.

CStdioFile sampleFile;
sampleFile.Open("test.txt",CFile::modeRead );
long length = 1;
CString row("");
while(sampleFile.ReadString(row))
{
    length++;
}

This isn't working. I'm not getting the correct value of the number of lines in the text file.What is wrong with this?

Thanks.

4

3 回答 3

2

To read Unicode text files you might want to check CStdioFile derived implementation: CStdioFileEx from codeproject:

http://www.codeproject.com/Articles/4119/CStdioFile-derived-class-for-multibyte-and-Unicode

于 2012-07-11T09:44:04.877 回答
1

尝试从 0 开始计数:

long length = 0;
于 2012-07-11T09:09:42.820 回答
1

length应该初始化为,0而不是1,因为您还没有阅读第一行:

CString row;
long length = 0;
while (sampleFile.ReadString(row))
{
    length++;
}
于 2012-07-11T09:08:14.927 回答