Solved

PROBLEM: conditional initialisation of stream

Posted on 1997-05-03
2
196 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 25 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

617 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