4

Possible Duplicate:
Which I/O library do you use in your C++ code?

I asked this question in a comment to another question, and I was asked to make it a proper question instead.

Why do I want to use iostream instead of stdio? More specifically, what do std::getline have to offer over the C equivalent?

Please, no language bashing.

Community
  • 1
  • 1
onemasse
  • 6,514
  • 8
  • 32
  • 37
  • 5
    [Which I/O library do you use in your C++ code? ](http://stackoverflow.com/questions/119098/which-i-o-library-do-you-use-in-your-c-code) has a good discussion of the trade-offs. – Matthew Flaschen Nov 16 '10 at 19:12

5 Answers5

11

There are several advantages, mostly with the << and >> operators. Getting a line isn't all that different, although being able to read it into a std::string is a considerable advantage.

C++ I/O has type safety. You don't write your parameter list as a quoted string, and then again as variables and such. You write what you're going to print once, and C++ figures out how many parameters and what type they are. When you have type mismatches, C I/O might get the I/O wrong, or even try to access protected memory.

C++ I/O is easily to extend. You can write operator<<() and operator>>() easily, once you've got a sample to copy. printf() and friends cannot be extended. You have a fixed list of format types.

C++ I/O, while it looks fairly simple at first, has a lot of programmer-accessible structure, and therefore a good C++ programmer can modify it to cover cases that C I/O can't. (Don't overuse this.)

GManNickG
  • 494,350
  • 52
  • 494
  • 543
David Thornley
  • 56,304
  • 9
  • 91
  • 158
4

The biggest gain is type safety. Format strings in C have no type (unlike in say, OCaml or boost::format) and so you can do some pretty nasty things with them by accident.

Niki Yoshiuchi
  • 16,883
  • 1
  • 35
  • 44
2
  1. You gain abstract input/output/seekable stream that can be implemented in any way.

    You write to /read from stream that may be file, memory, string or even custom filter or zlib compressor!

    Some C libraries provide an option for setting read/write handlers (BSD and Linux) but still do not have same power as std::streambuf and these are not standard.

  2. You may use stream specific locale allowing you to format data according to any locale to stream in thread specific way.

  3. Type safety.
  4. Writing to and reading from stream generic objects (complex variables, XML object etc.)

More?

Artyom
  • 31,019
  • 21
  • 127
  • 215
1

For one, if you use iostream then you get to work with std::string, rather then with char arrays, meaning that you have a lot less memory management to worry about.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • 1
    You can already use .c_str() in printf, with a std::string. std::string is nice, but it's independent of iostreams. – Chris Nov 16 '10 at 19:28
  • @Chris: There isn't anything for string *input*, though? – UncleBens Nov 16 '10 at 19:49
  • @Chris I was thinking specifically of `std::getline()`. It reads the line into an `std::string`, as opposed to `fgets()`, which reads the line into a character array. And with a char array you have to properly allocate it, keep track of its size, and deallocate it. – Dima Nov 16 '10 at 19:58
0

Well, if you use C++ you might want to use OOP, right? I guess cstdio (aka stdio.h) is there just for compatibility with C.

arifwn
  • 131
  • 8