954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

counting number of words in a file

Hi I am trying to count the number of words in a file and this is what i have so far. I am not sure about the getchar().. could you please tell me what i should be doing ?

#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;

int main() {
        string line;
        int size=0, count_words=0;
        char c;
        ifstream myfile("example.rtf");
        if(myfile.is_open()) {
                while (!myfile.eof()) {
                        getline(myfile,line);
                        cout<<line<<endl;
                        size++;

                }

                cout<<"number of lines "<<size<<endl;

        getchar(myfile,c);
        cout<<c<<endl;


        myfile.close();

        }

        else {
                cout<<"file not found"<<endl;
        }

return 0;
}
kavithabhaskar
Junior Poster
124 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

You should not be doing while (!myfile.eof()) -- here's why (feof() and .eof() are identical)

After you've read all the lines in the loop, what do you expect getchar() to read?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

You should not be doing while (!myfile.eof()) -- here's why (feof() and .eof() are identical)

After you've read all the lines in the loop, what do you expect getchar() to read?

Hi:

Thanks for ur reply.

Are you saying that i should use feof instead of eof ? I am not sure.

The other thing is I am not sure what I can do about that getchar. I am positive that what I have in my code is wrong. All I am able to see is that if there is a space, i should increment the counter by 1.

kavithabhaskar
Junior Poster
124 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

just keep counting every time it encounters a space

trying using isspace function I believe that calculates white spaces

while(isspace())
{
count++;
}

something like that.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

Hi:

Thanks for ur reply.

Are you saying that i should use feof instead of eof ? I am not sure.


If they are identical, what do you think? You need to look in your text how to loop through a file.The other thing is I am not sure what I can do about that getchar. I am positive that what I have in my code is wrong. All I am able to see is that if there is a space, i should increment the counter by 1.
Good, you know there is a problem. That's better than some others I've seen.
You need to think about what you are attempting to do. What designates the end of a word? What indicates the end of a line? Maybe in your loop instead of reading each line you could read each character looking for and counting these indicators.

See what you can do with that idea.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

I'm confused. Your question states that you want to count the number or "words" in a file, but your program is counting the number of "lines" in the file. Which one are you trying to accomplish?

That getchar() is totally useless there because you've already gotten to the end of the file and then you try to read a character, but there's nothing left to read.

My suggestions would be the following:

a) If you want to truly count the number of words in the file then do something like:

- Read the file one character at a time and count the spaces between the words
- Or read the file one word at a time and count each word.

b) If you want to count the number of lines in the file then:

while (getline(myfile, line)) { size++; }


Don't check for eof at all, because you don't need to. It will stop at the end of the file automatically, but do check if the file is open. Maybe something like " if (myfile.fail()) { cerr << "Could not open file\n"; } ".

necrolin
Posting Whiz in Training
225 posts since Jun 2009
Reputation Points: 105
Solved Threads: 26
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You