C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Daisy chaining streams

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
justaprogrammer@gmail.com
Guest





PostPosted: Wed Aug 17, 2005 12:45 pm    Post subject: Daisy chaining streams Reply with quote



I would like to connect two streams together in the following

bool operator>>(std::istream&i, CDate&d)
{
/*Cartoon of what I want to do....
std::ostringstream ost;
ost< std::string d = ost.str();
*/
//The following works
char c[100];
i.getline(c,99);
std::string d(c);
....
}

This operator is called by
vector //See Herb Sutter, Bjarne Stroustrup, etc...
copy ( istream_iterator<CDate> (cin),
istream_iterator<CDate>(),
back_inserter(e));

The user enters dates on the console by typing mm/dd/yy <Enter>. The
"working" code works fine but I do not like th dumb "99" cludge. The
cunning thing is that the getline waits until a '/n' (aka <Enter>) is
struck before continuing - so my cartoon will need a callback too.

I have done this previously by handrolling my own Sink classes - but I
feel streams should be able to do the same.

Any ideas?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Wed Aug 17, 2005 5:18 pm    Post subject: Re: Daisy chaining streams Reply with quote



[email]justaprogrammer (AT) gmail (DOT) com[/email] wrote:
Quote:
bool operator>>(std::istream&i, CDate&d)

Overloaded streaming operators should return the stream by reference, not a
boolean, so you can write:
CDate from, to;
std::cin >> from >> to;

Quote:
/*Cartoon of what I want to do....
std::ostringstream ost;
ost< std::string d = ost.str();
*/
//The following works
char c[100];
i.getline(c,99);
std::string d(c);

Here, I'd use the free getline() function:
std::string line;
if( std::getline( std::cin, line))
... // use line
else
... // handle IO error

Quote:
The
cunning thing is that the getline waits until a '/n' (aka struck before continuing - so my cartoon will need a callback too.

Every input requires the user to press enter, how else would you handle
backspaces without? I'm not sure what you mean with the callback.

Quote:
I have done this previously by handrolling my own Sink classes - but I
feel streams should be able to do the same.

I'm really not sure what you want here.

BTW: have you ever wondered what the 'C' in front of classes means? It's a
quirky old way used by the MFC to put their classes into a common namespace
- modern C++ has its own real namespaces.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.