reading numbers of words and lines in text file in C++

Discussion in 'C++' started by sahm, Dec 8, 2004.

  1. sahm

    sahm Guest

    does anyone know how to do it..i need help
    sahm, Dec 8, 2004
    #1
    1. Advertising

  2. sahm

    Tim Love Guest

    "sahm" <> writes:

    >does anyone know how to do it..i need help

    Show us the code you've written so far.
    Tim Love, Dec 8, 2004
    #2
    1. Advertising

  3. sahm

    sahm Guest

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>

    using namespace std;

    int main()
    {
    string fileName;
    char c;
    cout << endl;
    cout << "Enter the name of the file: ";
    cin >> fileName;
    cout << endl << endl;

    //declares filename
    ifstream d_file;
    d_file.open(fileName.c_str()); //attempts to open file

    if (!d_file.is_open())
    {
    //if file doesn't exist; don't create a new one
    cout << "File " << fileName << " does not exist in the
    client's current directory" << endl;
    system("pause");
    exit(1);
    }
    else
    {
    d_file.get(c);
    d_file.seekg(0, ios::end);
    int charCount = d_file.tellg();
    d_file.seekg (0, ios::beg);
    //prints the number of characters in a file;
    cout << "the number of characters in the " << fileName;
    cout << " is " << charCount << endl;

    //prints the number of lines in a file
    string t;
    int lineCount=0;

    while(getline(d_file, t, '\n'))
    ++lineCount;

    cout << "The number of lines in the file is " <<
    lineCount << endl;

    //reads the number of lines in a file, then print
    int wordCount=0;
    string word;

    for (;;)
    {
    d_file >> word;
    if ( d_file.eof() ) { break;}
    wordCount++;
    }
    cout << wordCount << endl;
    }
    d_file.close();
    system("pause"):
    return 0;
    }

    ******
    i got the read number of lines and characters in d file worked right
    except for the wordCount, it kept on giving me a 0....
    sahm, Dec 8, 2004
    #3
  4. sahm

    Rolf Magnus Guest

    sahm wrote:

    > #include <iostream>
    > #include <fstream>
    > #include <string>
    > #include <sstream>
    >
    > using namespace std;
    >
    > int main()
    > {
    > string fileName;
    > char c;
    > cout << endl;
    > cout << "Enter the name of the file: ";
    > cin >> fileName;
    > cout << endl << endl;
    >
    > //declares filename
    > ifstream d_file;
    > d_file.open(fileName.c_str()); //attempts to open file
    >
    > if (!d_file.is_open())
    > {
    > //if file doesn't exist; don't create a new one
    > cout << "File " << fileName << " does not exist in the
    > client's current directory" << endl;
    > system("pause");
    > exit(1);
    > }
    > else
    > {
    > d_file.get(c);
    > d_file.seekg(0, ios::end);
    > int charCount = d_file.tellg();
    > d_file.seekg (0, ios::beg);
    > //prints the number of characters in a file;
    > cout << "the number of characters in the " << fileName;
    > cout << " is " << charCount << endl;
    >
    > //prints the number of lines in a file
    > string t;
    > int lineCount=0;
    >
    > while(getline(d_file, t, '\n'))
    > ++lineCount;
    >
    > cout << "The number of lines in the file is " <<
    > lineCount << endl;
    >
    > //reads the number of lines in a file, then print
    > int wordCount=0;
    > string word;
    >
    > for (;;)
    > {
    > d_file >> word;
    > if ( d_file.eof() ) { break;}
    > wordCount++;
    > }
    > cout << wordCount << endl;
    > }
    > d_file.close();
    > system("pause"):
    > return 0;
    > }
    >
    > ******
    > i got the read number of lines and characters in d file worked right
    > except for the wordCount, it kept on giving me a 0....


    That's logical. You open a file, read it until the end to get the line
    count, then you try to read on to get the word count. Since you're already
    at the end of file, you break out of the loop immediately and the count
    stays at 0. Seek to the beginning and restet the stream's eof flag after
    the first loop.
    Rolf Magnus, Dec 8, 2004
    #4
  5. sahm

    rel0aded911

    Joined:
    Nov 23, 2009
    Messages:
    1
    hi!! can someone "translate" this on C cause i don't understand c++! thanks anyway. if he did i would appreciate it :):barresed:
    rel0aded911, Nov 23, 2009
    #5
    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. jazzy
    Replies:
    3
    Views:
    10,614
    Gordon Beaton
    May 25, 2004
  2. Joe Wright
    Replies:
    0
    Views:
    497
    Joe Wright
    Jul 27, 2003
  3. Murali
    Replies:
    2
    Views:
    543
    Jerry Coffin
    Mar 9, 2006
  4. Pali
    Replies:
    10
    Views:
    1,288
    Default User
    Nov 8, 2010
  5. Buck Turgidson

    Grep Text File for Lines Containing 1 or 2 Words

    Buck Turgidson, Jan 25, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    107
    John W. Krahn
    Jan 26, 2005
Loading...

Share This Page