468,491 Members | 2,079 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

detecting EOF using cin with file redirection

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 '06 #1
7 3292
iae...@yahoo.com wrote:
while (true)
{
cin >sInput;
if (!sInput.empty())
cout << sInput << endl;
}
The replies to this will be worth preserving...

/me sits back.

Tom

Jul 31 '06 #2
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 '06 #3
In article <11**********************@s13g2000cwa.googlegroups .com>,
<ia****@yahoo.comwrote:
>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
Jul 31 '06 #4
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 '06 #5
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
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.
Jul 31 '06 #6
Christopher Benson-Manica wrote:
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>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.
Aug 1 '06 #7
On Mon, 31 Jul 2006 17:33:31 UTC, ia****@yahoo.com 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!
Aug 2 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jole | last post: by
11 posts views Thread by Dag Sunde | last post: by
8 posts views Thread by Luciano A. Ferrer | last post: by
reply views Thread by Tom Gaudasinski | last post: by
4 posts views Thread by =?Utf-8?B?YzY3NjIyOA==?= | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.