472,808 Members | 3,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2589
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: las | last post by:
I'm having a wee problem with the get method, here is my code : ifstream infile; char x; infile.open("temp.txt"); if( !infile.good() ) { cout << "Error opening file" << endl; system("PAUSE");...
0
by: Montagna, Dan | last post by:
------_=_NextPart_001_01C35B55.62B4A6E0 Content-Type: text/plain; charset="iso-8859-1" Hello, I'm a very new mysql/php user and am trying to use the load data infile command without luck. I'd...
0
by: Donald Tyler | last post by:
Then the only way you can do it that I can think of is to write a PHP script to do basically what PHPMyAdmin is trying to do but without the LOCAL in there. However to do that you would need to...
2
by: Alex Hunsley | last post by:
I'm using a mysql monitor under cygwin (on win xp) to do a 'load data infile' to put some data into a mysql database (I'm using the xampp bundle).. My problem is that I have a four line CSV file...
3
by: Omid | last post by:
Hi. I have a piece of code that can be compiled and linked both with Cygwin g++ and with VC++. The code is: //WORKS WITH CYGWIN G++ //BUT NOT WITH CL.EXE (VC++) (compiles, but error when...
4
by: Siemel Naran | last post by:
My compiler (Borland C++) fails to compile this code: ifstream file(filename.c_str()); ostringstream out; file >> out.rdbuf(); with the error in the 3rd line above that: realmain.cpp(83):...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
2
by: kelvSYC | last post by:
Suppose I have something like this: class foo { std::istream in; public: foo(std::istream& in_) : in(in_.rdbuf()); void doStuffWith(std::ostream& out); void bar(); };
15
by: waltbrad | last post by:
Hello. I'm studying the book "C++ Primer Plus" by Stephan Prata. In chapter 6 he gives an exercise that reads from a file. The list is thus: 4 Sam Stone 2000 Freida Flass 100500 Tammy...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.