c++ Count Lines Of Text File

To retrieve an accurate line count on a text file use the following code.
[codesyntax lang=”cpp”]

int fBufFrame::countLines(string strIn)
{
    /*
        strIn variable contains the text file in a standard string
    */
    string str_to_search = "\n";
    int count = 0;
    /*
        Find the first ocurrance
    */
    int nPos = strIn.find(str_to_search, 0);
    while (nPos != string::npos)
    {
        count++;
        nPos = strIn.find(str_to_search, nPos + 1);
    }
    /*
        The variable count contins the number of lines in the file
    */
    return count;
}

[/codesyntax]

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

Your email address will not be published. Required fields are marked *