Ending input with an istream_iterator

D

Dave

#include <iostream>
#include <list>
#include <string>

using namespace std;

int main()
{
list<string> the_list(
(istream_iterator<string>(cin)),
istream_iterator<string>()
);
}


According to Jossutis, the start iterator will equal the end iterator when
an error or end-if-file occurs. How do I induce an end-of-file?

I simply cannot get this program to terminate normally because the call to
list's constructor never returns since I never finish iterating over the
range specified since I don't know how to induce an end-of-file!

Thanks,
Dave
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Dave said:
According to Jossutis, the start iterator will equal the end iterator when
an error or end-if-file occurs. How do I induce an end-of-file?

By entering an OS-dependent control character which signals it. In
Windows, use CTRL-Z + <newline>; in Linux, CTRL-D + <newline>.

#include <iostream>
#include <list>
#include <string>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
list<string> the_list((istream_iterator<string>(cin)),
istream_iterator<string>());
copy(the_list.begin(),
the_list.end(),
ostream_iterator<string>(cout, "\n"));
}

I noticed that, if any input is provided, it becomes necessary to enter
a newline before the actual eof code. If no input is provided, a CTRL-D
is enough to terminate the program in Linux; in Windows, it takes a
CTRL-Z + <newline>. I wonder whether this is all a matter of: (a)
SO-specific stuff; (b) implementation-depedent; or (c) a mixture of both.

Regards,
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top