Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

iostream

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.

like image 283
Brian T Hannan Avatar asked Jan 21 '10 17:01

Brian T Hannan


4 Answers

Streams are generally quite safe. Under some circumstances, they can be slow and/or clumsy. Slow, mostly stems from the fact that they impose a few extra layers between your code and the OS, and under the wrong circumstance those layers can add overhead. The clumsiness is mostly in comparison to C's printf, not direct use of something like WriteFile (which doesn't directly support formatting at all). For example, however, consider:

printf("%2.2x", ch);` 

to

std::cout << std::hex << std::setw(2) << std::setprecision(2) << std::setfill('0') << ch; 
std::cout << setfill(' ');

Then consider the fact that if you care about i18n, printf is using a string that's easy to read in from an external source, where the C++ stream is embedding all the formatting into the structure of the code, so nearly any change in formatting requires rewriting the code, recompiling and relinking.

CreateFile, ReadFile, etc, also allow a number of things like memory mapped files and overlapped reading and writing that aren't supported by iostreams at all. If the situation lets you make good use of these, iostreams often won't be competitive.

like image 115
Jerry Coffin Avatar answered Sep 18 '22 18:09

Jerry Coffin


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.

like image 38
luke Avatar answered Sep 22 '22 18:09

luke


You can't do asynchronous file i/o with streams ...

like image 33
Goz Avatar answered Sep 22 '22 18:09

Goz


  1. When you want your app to be portable to different platforms.

  2. When you want more succinct code: win32 functions have more elaborate semantics, often require a collection of functions to do something, and definitely have more parameters.

like image 27
Hassan Syed Avatar answered Sep 19 '22 18:09

Hassan Syed