bev
asked on
PROBLEM: conditional initialisation of stream
I have just spent a frustrating time sorting sorting out a problem that occurred in the following structure.....
#include <everything that's needed>
ostrstream os ;
main()
{
... snip
if (condition) ostrstream os (buf, 100, ios::out) ;
... snip
if (condition) os << "text string" ;
...snip...
}
What I found is that even with condition true, no data was written to the stream. When I took the ostrstream initialisation outside the conditional, it worked fine.
Appreciate that this may be judged a platform dependent query, but my first question is: does the initialisation of a stream inside a conditional violate any fundamental rule of C++? No (BC3.1) compiler errors or warnings were issued. My reference (Stroustrup) seems not to refer to the situation.
#include <everything that's needed>
ostrstream os ;
main()
{
... snip
if (condition) ostrstream os (buf, 100, ios::out) ;
... snip
if (condition) os << "text string" ;
...snip...
}
What I found is that even with condition true, no data was written to the stream. When I took the ostrstream initialisation outside the conditional, it worked fine.
Appreciate that this may be judged a platform dependent query, but my first question is: does the initialisation of a stream inside a conditional violate any fundamental rule of C++? No (BC3.1) compiler errors or warnings were issued. My reference (Stroustrup) seems not to refer to the situation.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER