473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Ending input with an istream_iterator

#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
Jul 23 '05 #1
1 2549
Dave wrote:
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,

--
Ney André de Mello Zunino
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Rudolph | last post by:
The member function basic_ios::operator!() returns the bool result of the basic_ios::fail() function which is true if either failbit or badbit is set (This is per p. 616 of TC++PL by B. Stroustrup...
4
by: Daniel Brewer | last post by:
Hi there, I would like to define a general operator>> function for valarrays that allows the input of an arbitary sized array from a file. An example data file would be like: 0 1 2 3 4 5 6 4 5...
9
by: Alex Vinokur | last post by:
------ foo.cpp : BEGIN ------ #include <cassert> #include <vector> #include <string> #include <iostream> #include <iterator> #include <fstream> using namespace std;
3
by: jmoy.matecon | last post by:
I get an error while compiling the following program: int main() { vector<int> v(istream_iterator<int>(cin), istream_iterator<int>()); copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));...
3
by: oliu321 | last post by:
I have the following code: vector<intA, B; copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(A)); copy(istream_iterator<int>(cin), istream_iterator<int>(),...
2
by: Juha Nieminen | last post by:
I'm using gcc 3.3.5. This code: std::set<std::stringt(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>()); gives a strange error message: error: cannot use...
10
by: Jess | last post by:
Hello, When I copy contents from an input string to a vector, I typically use something like this vector<stringv; ifstream in("file"); copy(istream_iterator<string(in),...
10
by: Jun | last post by:
hello, I've a data file ======== 4 1 1 1 1 5 1 1 1 1 1 2 1 1 ========
9
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.