473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 2624
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.