C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Writing to an unopened ofstream

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Gianluca Silvestri
Guest





PostPosted: Wed Sep 14, 2005 5:32 am    Post subject: Writing to an unopened ofstream Reply with quote




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





PostPosted: Wed Sep 14, 2005 2:40 pm    Post subject: Re: Writing to an unopened ofstream Reply with quote



"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.