Reading words with trailing whitespace

Discussion in 'C++' started by Brad Marts, Nov 14, 2003.

  1. Brad Marts

    Brad Marts Guest

    I am trying to read a file one word at a time, doing something with
    each word in between each read. When the file has trailing
    whitespace at the end of the last word, I wind up getting it twice.

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;

    int main()
    {
    string str;
    ifstream in("test.txt");

    while(!in.eof())
    {
    in >> str;
    // Do something with str, e.g.:
    cout << str << " ";
    }
    cout << "\n";
    in.close();
    return 1;
    }

    If "test.txt" is:
    this is a test

    with whitespace after test,

    The output is:
    this is a test test

    I know the problem is coming from the fact that I am not at eof
    after reading test and the next read doesn't alter the contents
    of str, but I'm not sure of how to make this work.

    Thanks,
    Brad
    Brad Marts, Nov 14, 2003
    #1
    1. Advertising

  2. "Brad Marts" <> wrote...
    > I am trying to read a file one word at a time, doing something with
    > each word in between each read. When the file has trailing
    > whitespace at the end of the last word, I wind up getting it twice.
    >
    > Code:
    > #include<iostream>
    > #include<fstream>
    > #include<string>
    > using namespace std;
    >
    > int main()
    > {
    > string str;
    > ifstream in("test.txt");
    >
    > while(!in.eof())
    > {
    > in >> str;
    > // Do something with str, e.g.:
    > cout << str << " ";
    > }
    > cout << "\n";
    > in.close();
    > return 1;
    > }
    >
    > If "test.txt" is:
    > this is a test
    >
    > with whitespace after test,
    >
    > The output is:
    > this is a test test
    >
    > I know the problem is coming from the fact that I am not at eof
    > after reading test and the next read doesn't alter the contents
    > of str, but I'm not sure of how to make this work.


    Check the result of the operator>> before doing anything with
    the 'str'. IOW:

    in >> str;
    if (in.good())
    cout << str << " ";
    else
    cout << "<DONE>";

    Victor
    Victor Bazarov, Nov 14, 2003
    #2
    1. Advertising

  3. Brad Marts

    Nudge Guest

    Brad Marts wrote:
    > I am trying to read a file one word at a time, doing something with
    > each word in between each read. When the file has trailing
    > whitespace at the end of the last word, I wind up getting it twice.


    Does this answer your question?
    http://www.parashift.com/c -faq-lite/input-output.html#faq-15.5
    Nudge, Nov 17, 2003
    #3
  4. Brad Marts

    Brad Marts Guest

    On Tue, 18 Nov 2003 00:00:51 +0100, Nudge wrote:

    > Brad Marts wrote:
    >> I am trying to read a file one word at a time, doing something with
    >> each word in between each read. When the file has trailing
    >> whitespace at the end of the last word, I wind up getting it twice.

    >
    > Does this answer your question?
    > http://www.parashift.com/c -faq-lite/input-output.html#faq-15.5


    Yes, it does. Thank you. Even after reading the faq for some time I still
    missed it.
    Brad Marts, Nov 17, 2003
    #4
    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. Donald Canton

    remove trailing whitespace from string

    Donald Canton, Feb 9, 2004, in forum: C++
    Replies:
    5
    Views:
    11,200
    Tilman Kuepper
    Feb 9, 2004
  2. KevinGPO
    Replies:
    1
    Views:
    340
    Richard Herring
    Feb 20, 2006
  3. Dotan Cohen
    Replies:
    2
    Views:
    505
    Dotan Cohen
    Oct 10, 2011
  4. Taylor Strait
    Replies:
    13
    Views:
    378
    Chris Gernon
    Dec 28, 2006
  5. Bob Smyph

    Remove only TRAILING whitespace

    Bob Smyph, Oct 14, 2008, in forum: Ruby
    Replies:
    4
    Views:
    155
    Bob Smyph
    Oct 14, 2008
Loading...

Share This Page