detecting EOF using cin with file redirection

I

iaesun

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
 
I

iaesun

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
 
W

Walter Roberson

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.
 
I

iaesun

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
 
C

Christopher Benson-Manica

Walter Roberson said:
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 :)
 
H

Herbert Rosenau

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top