468,537 Members | 1,912 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

infile.rdbuf() for empty file


I tried to print
cout << "<" << infile.rdbuf() << ">" << endl;
while infile is empty.

Here is what I have got.

<

I expected to get
<>

What is wrong?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #1
5 2362
On Wed, 2 Feb 2005 16:06:21 +0200 in comp.lang.c++, "Alex Vinokur"
<al****@big-foot.com> wrote,
I tried to print
cout << "<" << infile.rdbuf() << ">" << endl;
while infile is empty.


// compare:
cout << "<" << infile.rdbuf();
cout.clear();
cout << ">" << endl;

Jul 23 '05 #2

"David Harmon" <so****@netcom.com> wrote in message news:42***************@news.west.earthlink.net...
On Wed, 2 Feb 2005 16:06:21 +0200 in comp.lang.c++, "Alex Vinokur"
<al****@big-foot.com> wrote,
I tried to print
cout << "<" << infile.rdbuf() << ">" << endl;
while infile is empty.


// compare:
cout << "<" << infile.rdbuf();
cout.clear();
cout << ">" << endl;


OK.
But what happens to empty infile without cout.clear(); ?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #3
Alex Vinokur wrote:
I tried to print
cout << "<" << infile.rdbuf() << ">" << endl;
while infile is empty.

Here is what I have got.

<

I expected to get
<>

What is wrong?


The inserter for stream buffers is a somewhat odd creature:
it has no option to set any state flags on the input stream
since the state flags are members of the stream classes not
of the stream buffers. Thus, it reports failure to insert
at least one character, resulting from not obtaining one
character from the stream buffer in the first place, by
setting 'failbit' on the output stream. This is standard
conforming behaivor. You should clear the flag after
insertion if you are not interested in it, e.g. with a simple
manipulator:

/**/ template <typename cT, typename Tr>
/**/ std::basic_ios<cT, Tr>& clear(std::basic_ios<cT, Tr>&s)
/**/ {
/**/ s.clear();
/**/ return s;
/**/ }

/**/ std::cout << "<" << in.rdbuf() << clear << ">\n";
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4

"Dietmar Kuehl" <di***********@yahoo.com> wrote in message news:11*********************@o13g2000cwo.googlegro ups.com...
[nip]
/**/ template <typename cT, typename Tr>
/**/ std::basic_ios<cT, Tr>& clear(std::basic_ios<cT, Tr>&s)
/**/ {
/**/ s.clear();
/**/ return s;
/**/ }

/**/ std::cout << "<" << in.rdbuf() << clear << ">\n";

[snip]

If we know nothing of size of a file then a statement
cout << in.rdbuf() // without clear
can cause problem (?)

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #5
Alex Vinokur wrote:
If we know nothing of size of a file then a statement
cout << in.rdbuf() // without clear
can cause problem (?)


Yes. This insert is an oddball. I keep forgetting about this
nasty detail, too... You may file a defect against the standard
if you want to. However, I would argue that the current
behavior isn't a defect but works as designed. You would get
an official statement, though, and I can't always convince
people :-) Of course, you can always avoid the problems by
using

/**/ in >> std::cout.rdbuf();

instead. This should have the same behavior except that the
error flags are set on a different stream.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by las | last post: by
reply views Thread by Montagna, Dan | last post: by
reply views Thread by Donald Tyler | last post: by
3 posts views Thread by Omid | last post: by
4 posts views Thread by Siemel Naran | last post: by
2 posts views Thread by kelvSYC | last post: by
15 posts views Thread by waltbrad | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.