计算文本文件中的行数

时间:2012-07-11 09:03:11

标签: c++ mfc

我需要计算文本文件中的行数。这是我现在的代码。

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

这不起作用。我没有得到文本文件中行数的正确值。这有什么问题?

感谢。

3 个答案:

答案 0 :(得分:2)

要读取Unicode文本文件,您可能需要检查CStdioFile派生的实现:来自codeproject的CStdioFileEx:

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

答案 1 :(得分:1)

尝试从0开始计算:

long length = 0;

答案 2 :(得分:0)

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

CString row;
long length = 0;
while (sampleFile.ReadString(row))
{
    length++;
}
相关问题