infile.rdbuf() for empty file

A

Alex Vinokur

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?
 
D

David Harmon

On Wed, 2 Feb 2005 16:06:21 +0200 in comp.lang.c++, "Alex Vinokur"
I tried to print
cout << "<" << infile.rdbuf() << ">" << endl;
while infile is empty.

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

Alex Vinokur

David Harmon said:
On Wed, 2 Feb 2005 16:06:21 +0200 in comp.lang.c++, "Alex Vinokur"


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

OK.
But what happens to empty infile without cout.clear(); ?
 
D

Dietmar Kuehl

Alex said:
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";
 
A

Alex Vinokur

/**/ 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 (?)
 
D

Dietmar Kuehl

Alex said:
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.
 

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,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top