Topic: Writing to an unopened ofstream
Author: clcppmod-poster@not.a.valid.address ("Gianluca Silvestri")
Date: Wed, 14 Sep 2005 05:32:54 GMT Raw View
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 ]
Author: "kanze" <kanze@gabi-soft.fr>
Date: 14 Sep 2005 14:40:08 GMT Raw View
"Gianluca Silvestri" wrote:
> 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;
> }
Nothing. The badbit is set in os, but since you never test it,
that has no effect on anything.
> 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 ]