By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

infile.rdbuf() for empty file

P: n/a

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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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.