467,915 Members | 1,178 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Create program to count words in a file

I have a few documents in which I need to get a total word count.
Can anyone help?
I'd like to create this program so I can get the result after entering
the filename, and
have the option to enter another filename and get results again...
enter quit to end program.

Mar 7 '06 #1
  • viewed: 18067
Share:
9 Replies
This seems to accomplish what I need. Is it clean?

#include <iostream>
// #include <cassert> for Assert Fuction
#include <fstream>
#include <iostream>

using namespace std;

int main()
{

int count;
string fileName;
string astring;
ifstream inFile;

cout << "Enter the name of the file "
<< "(type Quit to end): ";
cin >> fileName;
while (fileName != "Quit")
{
inFile.open(fileName.c_str());

if (!inFile) // How do I use assert function here???
{
cout << "** Can't open the file **" << endl;
return 1;
}

count = 0;

while (inFile >> astring)
count++;

cout << "Number of words in file: " << count << endl;

inFile.close();
inFile.clear();

cout << "Enter the name of the file "
<< "(type Quit to end): ";
cin >> fileName;
}
return 0;
}

Mar 7 '06 #2
"aaron" <aa***@yadavis.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
This seems to accomplish what I need. Is it clean?

<snip>

See answer in alt.comp.lang.learn.c-c++

Mar 7 '06 #3
In article <1141723275.549806.324050
@i40g2000cwc.googlegroups.com>, aa***@yadavis.com says...
This seems to accomplish what I need. Is it clean?
Not particularly.
using namespace std;
Most experienced C++ programmers would consider this a
fairly ugly hack -- it's not necessarily terrible, but
certainly not particularly clean either.

[ ... ]
if (!inFile) // How do I use assert function here???


First of all, assert is a macro, not a function (an
important distinction in this case).

Second, assert isn't really suitable for this situation
anyway. It's basically intended to verifying program
logic -- i.e. if its condition is false, it indicates a
problem in your program, rather than data that's supplied
to the program. This is looking at the input data, so an
'if' is a perfectly reasonable way to handle it.

For real use, an interactive program like this that
requires the user to type in file names as it runs is
rarely as useful as a program that accepts file names on
the command line so the user can specify all of them up-
front. Along with that, I'd move the counting into a
separate function:

int count(char const *filename) {
int words = 0;
std::string word;
std::ifstream infile(filename);

if (!infile)
return -1;

while (infile >> word)
++words;

return words;
}

int main(int argc, char **argv) {

for (int i=1; i<argc; i++) {
int words = count(argv[i]);

std::cout << argv[i] << ": ";

if (words < 0)
std::cout << "Unable to open file!\n";
else
std::cout << count(argv[i]) << "\n";
}
return 0;
}

One other possibility would be to use std::distance
instead of an explicit loop to count the words in the
file:

int count(char const *filename) {
std::ifstream infile(filename);

if (!infile)
return -1;

return std::distance(
std::istream_iterator<std::string>(infile),
std::istream_iterator<std::string>());
}

Side note: A typical UNIX shell will expand wildcards and
such, so this will automatically support things like "wc
*.txt". Most shells in Windows don't do that, but most
compilers include code to do it that you can link into
your program if you want it (its name varies from one
compiler to the next though -- e.g. Microsoft calls it
setargv.obj, Borland calls it wildargs.obj, and so on).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 7 '06 #4

Jerry Coffin wrote:
In article <1141723275.549806.324050
@i40g2000cwc.googlegroups.com>, aa***@yadavis.com says...
This seems to accomplish what I need. Is it clean?
Not particularly.
using namespace std;


Most experienced C++ programmers would consider this a
fairly ugly hack -- it's not necessarily terrible, but
certainly not particularly clean either.

[ ... ]


do you mean 'using namespace std;' is an ugly hack?...
if (!inFile) // How do I use assert function here???


First of all, assert is a macro, not a function (an
important distinction in this case).

Second, assert isn't really suitable for this situation
anyway. It's basically intended to verifying program
logic -- i.e. if its condition is false, it indicates a
problem in your program, rather than data that's supplied
to the program. This is looking at the input data, so an
'if' is a perfectly reasonable way to handle it.

For real use, an interactive program like this that
requires the user to type in file names as it runs is
rarely as useful as a program that accepts file names on
the command line so the user can specify all of them up-
front. Along with that, I'd move the counting into a
separate function:

int count(char const *filename) {
int words = 0;
std::string word;
std::ifstream infile(filename);

if (!infile)
return -1;

while (infile >> word)
++words;

return words;
}

int main(int argc, char **argv) {

for (int i=1; i<argc; i++) {
int words = count(argv[i]);

std::cout << argv[i] << ": ";

if (words < 0)
std::cout << "Unable to open file!\n";
else
std::cout << count(argv[i]) << "\n";
}
return 0;
}

One other possibility would be to use std::distance
instead of an explicit loop to count the words in the
file:

int count(char const *filename) {
std::ifstream infile(filename);

if (!infile)
return -1;

return std::distance(
std::istream_iterator<std::string>(infile),
std::istream_iterator<std::string>());
}

Side note: A typical UNIX shell will expand wildcards and
such, so this will automatically support things like "wc
*.txt". Most shells in Windows don't do that, but most
compilers include code to do it that you can link into
your program if you want it (its name varies from one
compiler to the next though -- e.g. Microsoft calls it
setargv.obj, Borland calls it wildargs.obj, and so on).

--
Later,
Jerry.

The universe is a figment of its own imagination.


Mar 7 '06 #5
Fei Liu wrote:
do you mean 'using namespace std;' is an ugly hack?...


The C++ thought leaders formerly said it was, and have since softened their
stance to "mostly harmless".

This repost best describes the issues involved:

Do you remember the scene in Star Trek Old Generation where they could not
get the hatch to a grain silo open, and when Kirk finally opened it
thousands of tribbles rained down all over him?

Kirk represents your source file.

The grain silo represents all the header files your source file includes.

The tribbles each represents an identifier declared inside 'namespace std'
in those headers.

Raining down all over Kirk represents all those identifiers polluting your
local namespace.

'using namespace std' represents sliding the hatch open.

The purpose of the 'namespace' keyword is to prevent this pollution. You
must keep all the tribbles in the grain silo, and only take down the one or
two that you need:

using std::cout;
using std::endl;

Folks use 'using namespace std' in this newsgroup because trivial example
code often uses it; the code is not large enough to have enough of its own
identifiers to potentially conflict with the 'std' ones. But nobody should
use 'using namespace std', and those who post sample code to this newsgroup
should set a good example.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 7 '06 #6
Fei Liu wrote:
Jerry Coffin wrote:
In article <1141723275.549806.324050
@i40g2000cwc.googlegroups.com>, aa***@yadavis.com says...
This seems to accomplish what I need. Is it clean?


Not particularly.

using namespace std;


Most experienced C++ programmers would consider this a
fairly ugly hack -- it's not necessarily terrible, but
certainly not particularly clean either.

[ ... ]

do you mean 'using namespace std;' is an ugly hack?...

if (!inFile) // How do I use assert function here???

[...and more than 60 lines quoted without merit...]


Please learn to quote only the relevant portions. Thanks.

V
Mar 7 '06 #7
In article <1141771485.909619.196510
@i39g2000cwa.googlegroups.com>, fe*****@gmail.com says...

[ ... ]
Most experienced C++ programmers would consider this a
fairly ugly hack -- it's not necessarily terrible, but
certainly not particularly clean either.

[ ... ]


do you mean 'using namespace std;' is an ugly hack?...


I mean "using namespace std;" is a _fairly_ ugly hack
when it's at file scope. I think without qualifications,
it's overstated, and I think that at other scopes it's
somewhat more acceptable (though still rarely ideal).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 8 '06 #8

Phlip wrote:
Fei Liu wrote:
do you mean 'using namespace std;' is an ugly hack?...


The C++ thought leaders formerly said it was, and have since softened their
stance to "mostly harmless".

This repost best describes the issues involved:

Do you remember the scene in Star Trek Old Generation where they could not
get the hatch to a grain silo open, and when Kirk finally opened it
thousands of tribbles rained down all over him?

Kirk represents your source file.

The grain silo represents all the header files your source file includes.

The tribbles each represents an identifier declared inside 'namespace std'
in those headers.

Raining down all over Kirk represents all those identifiers polluting your
local namespace.

'using namespace std' represents sliding the hatch open.

The purpose of the 'namespace' keyword is to prevent this pollution. You
must keep all the tribbles in the grain silo, and only take down the one or
two that you need:

using std::cout;
using std::endl;

Folks use 'using namespace std' in this newsgroup because trivial example
code often uses it; the code is not large enough to have enough of its own
identifiers to potentially conflict with the 'std' ones. But nobody should
use 'using namespace std', and those who post sample code to this newsgroup
should set a good example.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


Thanks for your explanation, Phlip. I understand the reason why
indiscrimnately 'using namespace std' is a bad idea. But it was the
first time I heard it being referred to as 'an ugly hack'. It is
interesting they later relaxed their phrase.

V, as you may or may not be able to see, I wasn't sure what was being
quoted in Phlip's original post as 'an ugly hack' thus my quote of
Phlip's complete message.

Mar 8 '06 #9
thanks Phlip!!!!

Mar 8 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Ling Lee | last post: by
5 posts views Thread by name | last post: by
9 posts views Thread by santosh | last post: by
66 posts views Thread by genestarwing | last post: by
3 posts views Thread by waynejr25 | last post: by
5 posts views Thread by Snaggy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.