# Mixing stdio and iostream

## Recommended Posts

I usually use C++ stream objects like cout and cin for conole I/O, but the library I started using requires C style I/O. Is it ok to mix them? For example, I currently have something like
        cout << "Enter a big number.\n";
mpz_inp_str( n, stdin, 0);
cout << "You entered\n";
mpz_out_str( stdout, 10 , n);
cout << "\nIn Hexadeciaml, that is\n0x";
mpz_out_str( stdout, -16 , n);
cout << "\n";


It appears to work, but that isn't always a guarantee that it will in the future.

##### Share on other sites
Quote:
 Original post by StoryyellerIs it ok to mix them?

I think so, it may not make for pretty code, but there shouldn't be any execution problems, cin, cout and cerr encapsulate stdin, stdout and stderr, so it is not like you're really having separate streams.

##### Share on other sites
Quote:
Original post by Kwizatz
Quote:
 Original post by StoryyellerIs it ok to mix them?
I think so, it may not make for pretty code, but there shouldn't be any execution problems, cin, cout and cerr encapsulate stdin, stdout and stderr, so it is not like you're really having separate streams.
It is guaranteed to work, AFAIK. However, mixing stdio and iostreams can have unfortunate effects on the performance of the iostreams, because they have to do extra work to keep the buffering correct.

##### Share on other sites
Guaranteed by standard.

##### Share on other sites
By default the streams are synchronised, so it's (supposedly) guaranteed to work by the standard. The syncing can be toggled using sync_with_stdio.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628336
• Total Posts
2982156

• 9
• 24
• 9
• 9
• 13