Brian T Hannan Brian T Hannan - 1 month ago 10
C++ Question

When is it good to use c++ iostreams over ReadFile, WriteFile, fprintf, etc ...?

I find that it is tremendously easier to use streams in c++ instead of windows functions like ReadFile, WriteFile, etc or even fprintf. When is it not good to use streams? When is it good to use streams? Is it safe to use streams? How come a lot of programmers don't use streams?

This is just something I've always wondered about and maybe you can shed some wisdom.

Answer

When is it not good to use streams?

  • Streams are not guaranteed to be thread safe. It's easy to dream up a situation where you can not use streams without some synchronization.
  • Stream objects are typically pretty "heavy". They may be too heavy for low memory or embedded environments.

When is it good to use streams?

In general.

Is it safe to use streams?

Yes, but you've got to be careful when sharing a stream asynchronously.

How come a lot of programmers don't use streams?

Preference, style, or they learned a different method (or different language) first. I find that plenty of old "c++" examples online are written with a C-flavor to them, prefering printf to cout.

Comments