 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gianluca Silvestri Guest
|
Posted: Wed Sep 14, 2005 5:32 am Post subject: Writing to an unopened ofstream |
|
|
Hi guys,
Could anyone tell me what is the effect of the code below, according to the
Standard?
#include <fstream>
#include <ostream>
int main()
{
std::ofstream os;
os << "Hello world!" << std::endl;
}
Is this UB? Or should the operator<< fail?
Thanks
---
Gianluca Silvestri
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Sep 14, 2005 2:40 pm Post subject: Re: Writing to an unopened ofstream |
|
|
"Gianluca Silvestri" wrote:
| Quote: | Could anyone tell me what is the effect of the code below,
according to the Standard?
#include
#include
int main()
{
std::ofstream os;
os << "Hello world!" << std::endl;
}
|
Nothing. The badbit is set in os, but since you never test it,
that has no effect on anything.
| Quote: | Is this UB? Or should the operator<< fail?
|
No undefined behavior. The operator<< calls sputc on the
filebuf, which (the first time, at least) calls overflow, which
returns EOF is ! is_open(). By convention, any time sputc
returns EOF, an output stream sets badbit.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|