How to count lines of a file in C++?

后端 未结 7 1137
故里飘歌
故里飘歌 2020-12-02 15:53

How can I count lines using the standard classes, fstream and ifstream?

7条回答
  •  -上瘾入骨i
    2020-12-02 16:31

    This is the correct version of Craig W. Wright's answer:

    int numLines = 0;
    ifstream in("file.txt");
    std::string unused;
    while ( std::getline(in, unused) )
       ++numLines;
    

提交回复
热议问题