vsz
vsz

Reputation: 4883

Are there technical dangers in mixing fstream and stdio?

It is very clear that it's not a good practice to mix iostream/fstream with stdio and C-style i/o handling.

Printing one line with printf(...) and another one with std:cout << ..., or reading a file with FILE* and later writing with ofstream is ugly, can create confusion and is just asking for trouble.

However, is the only reason such practice is frowned upon basically just a stylistic / readability argument, or does it have deeper technical reasons?

I'm asking this because I need mmap in a small part of my code, doing some low-level register handling. mmap works with c-style file descriptors. However, in the rest of the code, I would like to go with the C++ stream route for handling files.

Upvotes: 3

Views: 196

Answers (1)

MSalters
MSalters

Reputation: 179930

The technical danger is decreased performance, as the two methods of output cannot be buffered independently. There's ios_base::sync_with_stdio(bool) to indicate such synchronization isn't needed (for instance because <cstdio> isn't used), but the default is slow and correct.

Upvotes: 3

Related Questions