Software & Finance





Visual C++ - Counting Characters, Words and lines from a file or string buffer





Here I have given the source code to count the number of Characters, Words and lines from a file or string buffer.

 

GetWordCount function is written in C++ language and tested with visual c++ compiler.

 


Source Code


// WordCount.cpp : Defines the entry point for the console application.

//

 

#include <iostream>

#include <fstream>

 

bool GetWordCount2(const char *srcFileName, int &numChars, int &numWords, int &numLines)

{

    FILE *istream = fopen(srcFileName, "rb");

 

    if (istream == 0)

        return false;

 

    numChars = 0;

    numWords = 0;

    numLines = 0;

 

    const int len = 4096;

    char buf[4096];

 

    do

    {

        if( feof(istream))

            break;

        int nBytesRead = fread(buf, 1, len, istream);

        if(nBytesRead <= 0)

            break;

        int nc, wc, lc;

        GetWordCount(buf, nBytesRead, nc, wc, lc);

        numChars += nc;

        numWords += wc;

        numLines += lc;

 

    } while(1);

 

    fclose(istream);

    return true;

}

 

 bool GetWordCount(const char *buf, int len, int &numChars, int &numWords, int &numLines)

{

    numChars = len;

    numWords = 0;

    numLines = 0;

 

    int prevId = -1;

 

    for(int i = 0; i < len; i++)

    {

        if(buf[i] == '\n')

            numLines++;

 

        // If you want, you can include comma (,) or any

        // other characters that can separate words

        if(buf[i] == ' ' || buf[i] == '\n' ||

            buf[i] == '\r' || buf[i] == '\t')

        {

            // To Skip continuos white space character.

            // You can comment this if block, if you do not to count

            // successive white space character for one word.

            if(prevId == i - 1)

            {

                prevId = i;

                continue;

            }

            prevId = i;

            numWords++;

        }

    }

 

    return true;

}

 

bool GetWordCount(const char *srcFileName, int &numChars, int &numWords, int &numLines)

{

    numChars = 0;

    numWords = 0;

    numLines = 0;

 

    std::ifstream ifs(srcFileName, std::ios::binary | std::ios::in);  

 

    if(ifs.is_open() == false)

        return false;

 

    const int len = 4096;

    char buf[4096];

    while(1)

    {

        if(ifs.eof())

            break;

        ifs.read(buf, len);

        int nBytesRead = ifs.gcount();

        if(nBytesRead <= 0)

            break;

        int nc, wc, lc;

        GetWordCount(buf, nBytesRead, nc, wc, lc);

        numChars += nc;

        numWords += wc;

        numLines += lc;

    }

    ifs.close();

 

    return true;

}

 

int main()

{

    int nc, wc, lc;

    GetWordCount("c:\\kathir.txt", nc, wc, lc);

 

    std::cout << "Chars: " << nc << "\n";

    std::cout << "Words: " << wc << "\n";

    std::cout << "Lines: " << lc << "\n";

 

    return 0;

}

 

Output


 

The file kathir.txt contains the following lines:

Welcome to softwareandfinance.com website.
Thank you very much for using our web site.

 

The output is given below:

 

Chars: 87

Words: 13

Lines: 2

Press any key to continue . . .