Synchronizing iostream with stdio

I am trying to add iostream to legacy code and hence want to keep the two libraries in sync. According to this article, I have to use std :: ios_base :: sync_with_stdio.

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

thank

+2


source to share


2 answers


Threads are synchronized by default, they guarantee to work as standard, you don't need to do anything. sync_with_stdio

is only intended to disable sync if you want.

From the article mentioned:



For predefined streams, it is safe to mix stdio and iostreams. For example, you can safely use stdin and cin in the same program; The C ++ Standard ensures that it will work as you naively expect it to.

The only downside is potential performance hit (I think why it might be disabled).

+7


source


As TheSamFrom1984 says, default sync is not a problem. However, synchronization only makes sense when the same streams are used by both libraries. This usually happens when using cin / cout / cerr and stdin / stdout / stderr respectively. However, I can see several reasons why both should be used at the same time, aside from reusing legacy code.



When I first started using C ++ I found myself doing this because often I knew how to do something with stdio but didn't know how to do it with iostream, but the best approach is to figure out how do it one or the other, but not both.

+3


source







All Articles