how to count words in a text file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: how to count words in a text file

  1. #1
    Join Date
    Nov 2010
    Posts
    139

    how to count words in a text file

    hi i have this file with words printed on seperate lines. i want to count these words and display the total on the screen. at present i have a function to do this however it doesn't seem to be working, it outputs to screen:
    word count: 0.
    word count: 0.
    word count: 0. repeatedly on seperate rows always saying 0, when basically i want it to say for example, word count: 50 just one time as opposed to 50 times can anyone help me figure out why its not doing that. thanks.

    Code:
    int wordcount(){
    
    ifstream inFile;
    
    inFile.open("C:\\Users\\hp\\Desktop\\egg.txt");
    if (inFile.is_open())
    {
    	string word;
    	unsigned long wordCount = 0;
    	while(!inFile.eof())
    	{
    		inFile >> word;
    		if(word.length() > 0)
    		{
    			wordCount++;
    		}
    	}
    	cout << "Unique word count: " << wordCount << endl;
    }
    return 0;
    
    }
    Last edited by katy_price; June 11th, 2011 at 06:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center