Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PROBLEM: conditional initialisation of stream

Posted on 1997-05-03
2
Medium Priority
?
197 Views
Last Modified: 2010-04-10
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.

0
Comment
Question by:bev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1163255
You declared *two* streams, both named "os": one as a global
variable, and the other local to main(). In main(), the local os
hides the global variable os. Your "text string" is writen to
the local os.
What you need to do instead of
    if (condintion) ostrstream os(buf, 100, ios::out) ;
is
    if (condition) os.rdbuf().setbuf(buf, 100);
which attaches buf to the streambuf object contained by os.

Hope this helps.
0
 

Author Comment

by:bev
ID: 1163256
Thanks. You've helped a lot. Bruce
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question