14

When I search on the Internet for the difference between these two libraries, everyone says that <iostream> is the standard I/O library of C++ and <cstdio> is for C.

My professor says that cin>> and cout<< are not good functions and if we use cin>> many times, our application will definitely crash. He also says that stdio provides nearly three times faster input and output than iostream. However, I prefer using iostream because it is more convenient, and also I don't know if my professor is right.

What do you advise me to use?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam379
  • 271
  • 2
  • 4
  • 13

4 Answers4

17

Using iostream should not make your program crash. It can be slow, but that's only because it's trying to interoperate with stdio. That synchronization can be turned off1. iostream is the idiomatic C++ way to get input, and I'd recommend its use over stdio functions in most cases when using C++.

1 using std::ios::sync_with_stdio(false);

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • @Sam379 You can also read this post http://stackoverflow.com/questions/9371238/why-is-reading-lines-from-stdin-much-slower-in-c-than-python?rq=1 – log0 Jun 25 '13 at 06:03
  • 3
    There are situations where stdio is preferable; this kind of blanket statement isn't necessarily helpful... – Oliver Charlesworth Jun 25 '13 at 06:04
  • @OliCharlesworth, And even better, we can have type safe versions, decreasing some of the danger of using the C ones. – chris Jun 25 '13 at 06:06
  • @Oli: I imagine there are indeed such situations, but I can't immediately think of one; would you mind giving an example or two of situations where `stdio` might be preferable? – icktoofay Jun 25 '13 at 06:08
  • 1
    @icktoofay: Well, performing formatted I/O is usually (but not always) more succinct and clearer when using `printf` than messing around with stream manipulators. – Oliver Charlesworth Jun 25 '13 at 06:09
  • 1
    @OliCharlesworth `Boost.Format`. Also, clearer in what terms? The fact that specification of `sprintf` makes it essentially impossible to write reasonable code with it (unknown output length) isn't really welcoming (i.e. You can use it and the syntax is shorter, but it will crash). Now *that's* messing around. – Bartek Banachewicz Jun 25 '13 at 07:24
9

Use streams in C++ and stdio.h in C. Yes, streams are a bit slower, but do those milliseconds count? User input is rarely a bottleneck of an application.

And if streams are used properly, and your compiler/runtime libraries are ok, your application won't crash.

But, if you have good, explainable reasons to use cstdio functions, then it is fully legitimate to use them in C++ as well.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • 4
    It's not just user input, it's any interaction with files... – Oliver Charlesworth Jun 25 '13 at 06:04
  • 1
    @OliCharlesworth: you are completely right, I've written "input" because the question goes primarily about `cin`. – Alex Shesterov Jun 25 '13 at 06:11
  • 1
    This is misleading. Any program that produces an even moderately sized output will see a great difference between using cout and printf – andrepd Mar 01 '16 at 14:00
  • 1
    @andrepd, this depends on the definitions of "moderately sized" (output) and "great" (difference). For tools like `tail`, it surely matters, for business applications it _usually_ doesn't. – Alex Shesterov Mar 01 '16 at 22:36
3

Unless performance of the I/O really matters, use whichever makes your program the clearest (easiest to read).

In the vast number of programs I've written, only a few have needed special treatment to "how fast the I/O is" - and most of the problem with std::stream functions has to to with the actual parsing of the input (as well as sync with stdio) - which, if you are reading, say, floating point numbers, will be quite difficult to write your own version of (that accepts the full range of formats that std::stream allows).

If I/O performance really matters, then using std::stream::read and std::stream::write may be the solution, but in most cases, best performance comes from using the non-portable mmap and MapViewOfFile interfaces that "map" the contents of a file directly from the filesystem to the virtual memory of the application. This saves on the amount of copying the processing of the data takes, and will make it a little faster.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

The iostreams library probably is slower than the lower-level stdio library. Streams does a lot more under the covers - type conversions, localization, exception handling, etc.

jwismar
  • 12,164
  • 3
  • 32
  • 44