views:

104

answers:

2

I am trying to add iostream to the legacy code and thus want to sync those two libraries. According to this article, I should use std::ios_base::sync_with_stdio.

Now, I wonder how it is used in practice (examples please), side-effects I should be aware of.

Thx

+2  A: 

By default the streams are synchronized, it's guaranteed to work by the standard, you don't have to do anything. sync_with_stdio is only here to disable synchronisation if you want to.

From the article you mentioned :

For the predefined streams, it's safe to mix stdio and iostreams. For example, you can safely use stdin and cin in the same program; the C++ Standard guarantees that it will work the way you would naively expect it to.

The only drawback is a potential performance hit (I guess that's why it can be disabled).

Samuel_xL
A: 

As TheSamFrom1984 says, synced is the default so it should not be a problem. However synchronisation is only relevant when the same stream is being operated on by both libraries. This typically occurs when using cin/cout/cerr and stdin/stdout/stderr respectively. However I can see few reasons for needing to use both simultaneously except when reusing legacy code.

When I first started using C++ I found myself doing this because often I knew how to do something using stdio, but did not know how to do it with iostream, but a better approach would be to figure out how to do in in one or the other, but not both.

Clifford