detecting EOF using cin with file redirection

Discussion in 'C Programming' started by iaesun@yahoo.com, Jul 31, 2006.

  1. Guest

    hello,

    this post assumes executing a program with file redirection as follows:

    program.exe < intput.txt

    where input.txt is a text file where there is not necessarily any
    whitespace after the last character and the EOF.

    i've found some useful posts about detecting EOF when using cin with
    file redirection, but they have so far all been analyzing the input
    string character by character.

    i was wondering if there was a way to do it word by word instead?

    for example the follow code snippet:

    while (true)
    {
    cin >> sInput;
    if (!sInput.empty())
    cout << sInput << endl;
    }

    prints out each word just fine so long as there is white space after
    the last word of the file. if there is no white space, it just keeps
    processing the last word over and over in an infinite loop.

    now if i was reading in each character, i could test for EOF, but since
    i'm not, i'm wondering how to do the same basic thing using the handy
    built-in white space reading of cin.

    thanks for any help,

    jason
    , Jul 31, 2006
    #1
    1. Advertising

  2. Tom St Denis Guest

    wrote:
    > while (true)
    > {
    > cin >> sInput;
    > if (!sInput.empty())
    > cout << sInput << endl;
    > }


    The replies to this will be worth preserving...

    /me sits back.

    Tom
    Tom St Denis, Jul 31, 2006
    #2
    1. Advertising

  3. Guest

    haha, yes yes, i realize now i forgot the else break;

    :)

    it's actually in the code that i'm executing however, so hopefully
    someone will read this reply and answer the original question, which
    still stands.

    thanks for pointing it out though,

    jason
    , Jul 31, 2006
    #3
  4. In article <>,
    <> wrote:

    >i've found some useful posts about detecting EOF when using cin with
    >file redirection,


    Sorry, our dear son, C++, moved out of the house more
    than 20 years ago. He's all grown up now, and lives in his
    own newsgroup, comp.lang.c++ . He got a lot of his looks from
    is father, Bjorne, but there's still enough family resemblance
    that a lot of people get him confused with us.
    --
    All is vanity. -- Ecclesiastes
    Walter Roberson, Jul 31, 2006
    #4
  5. Guest

    oh crap, i'm sorry, i didn't even realize i was posting in c instead of
    c++, thanks for bringing that to my attention
    , Jul 31, 2006
    #5
  6. Walter Roberson <-cnrc.gc.ca> wrote:

    > own newsgroup, comp.lang.c++ . He got a lot of his looks from
    > is father, Bjorne, but there's still enough family resemblance


    While he may have been Bjorne of C, I'm pretty sure he goes by Bjarne
    these days :)

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jul 31, 2006
    #6
  7. Christopher Benson-Manica wrote:
    > Walter Roberson <-cnrc.gc.ca> wrote:
    >
    >> own newsgroup, comp.lang.c++ . He got a lot of his looks from
    >> is father, Bjorne, but there's still enough family resemblance

    >
    > While he may have been Bjorne of C, I'm pretty sure he goes by Bjarne
    > these days :)
    >

    I hear he's quite the bear, anyway.
    Clever Monkey, Aug 1, 2006
    #7
  8. On Mon, 31 Jul 2006 17:33:31 UTC, wrote:

    > hello,
    >
    > this post assumes executing a program with file redirection as follows:
    >
    > program.exe < intput.txt
    >
    > where input.txt is a text file where there is not necessarily any
    > whitespace after the last character and the EOF.
    >
    > i've found some useful posts about detecting EOF when using cin with
    > file redirection, but they have so far all been analyzing the input
    > string character by character.
    >
    > i was wondering if there was a way to do it word by word instead?
    >
    > for example the follow code snippet:
    >
    > while (true)
    > {
    > cin >> sInput;


    This is not C. C++ is the next door.


    --
    Tschau/Bye
    Herbert

    Visit http://www.ecomstation.de the home of german eComStation
    eComStation 1.2 Deutsch ist da!
    Herbert Rosenau, Aug 2, 2006
    #8
    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. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,085
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,120
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,646
    Alex Vinokur
    Mar 6, 2005
  4. Ralf Goertz

    redirection of cin/cout

    Ralf Goertz, Nov 24, 2006, in forum: C++
    Replies:
    2
    Views:
    331
    Ralf Goertz
    Nov 27, 2006
  5. Fernando
    Replies:
    4
    Views:
    1,631
    Fernando
    Nov 16, 2011
Loading...

Share This Page