Create program to count words in a file

Discussion in 'C++' started by aaron, Mar 7, 2006.

  1. aaron

    aaron Guest

    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.
    aaron, Mar 7, 2006
    #1
    1. Advertising

  2. aaron

    aaron Guest

    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;
    }
    aaron, Mar 7, 2006
    #2
    1. Advertising

  3. aaron

    Jim Langston Guest

    "aaron" <> wrote in message
    news:...
    > This seems to accomplish what I need. Is it clean?

    <snip>

    See answer in alt.comp.lang.learn.c-c++
    Jim Langston, Mar 7, 2006
    #3
  4. aaron

    Jerry Coffin Guest

    In article <1141723275.549806.324050
    @i40g2000cwc.googlegroups.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);

    std::cout << argv << ": ";

    if (words < 0)
    std::cout << "Unable to open file!\n";
    else
    std::cout << count(argv) << "\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.
    Jerry Coffin, Mar 7, 2006
    #4
  5. aaron

    Fei Liu Guest

    Jerry Coffin wrote:
    > In article <1141723275.549806.324050
    > @i40g2000cwc.googlegroups.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);
    >
    > std::cout << argv << ": ";
    >
    > if (words < 0)
    > std::cout << "Unable to open file!\n";
    > else
    > std::cout << count(argv) << "\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.
    Fei Liu, Mar 7, 2006
    #5
  6. Fei Liu wrote:
    > Jerry Coffin wrote:
    >
    >>In article <1141723275.549806.324050
    >>@i40g2000cwc.googlegroups.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
    Victor Bazarov, Mar 7, 2006
    #6
  7. aaron

    Phlip Guest

    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!!!
    Phlip, Mar 7, 2006
    #7
  8. aaron

    Jerry Coffin Guest

    In article <1141771485.909619.196510
    @i39g2000cwa.googlegroups.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.
    Jerry Coffin, Mar 7, 2006
    #8
  9. aaron

    Fei Liu Guest

    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.
    Fei Liu, Mar 7, 2006
    #9
  10. aaron

    aaron Guest

    thanks Phlip!!!!
    aaron, Mar 8, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Heathfield
    Replies:
    7
    Views:
    358
    Barry Schwarz
    Oct 5, 2003
  2. Replies:
    4
    Views:
    327
    Pete Becker
    Feb 8, 2007
  3. Umesh
    Replies:
    17
    Views:
    780
    James Kanze
    Apr 26, 2007
  4. Umesh
    Replies:
    25
    Views:
    1,554
    James Kanze
    Apr 26, 2007
  5. akshar108 via DotNetMonster.com

    Count words of Pdf file

    akshar108 via DotNetMonster.com, Sep 17, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    321
    akshar108 via DotNetMonster.com
    Sep 17, 2007
Loading...

Share This Page