What can C ++ iostreams offer compared to the C stdio library?

Possible duplicate:
What I / O library do you use in your C ++ code?

I asked this question in a comment on another question, and I was asked to ask him the right question.

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

Please do not beat the tongue.

+4
source share
5 answers

There are several advantages, mainly with << and >> operators. Getting a string is not that different, although being able to read it in std::string is a significant advantage.

C ++ I / O has a security type. You do not write your parameter list as a quoted string, and then again as variables, etc. You write what you are going to print once, and C ++ determines how many parameters and what type they are. If you have type mismatches, CI / O can lead to incorrect I / O or even try to access protected memory.

C ++ I / O is easily extensible. You can easily write operator<<() and operator>>() as soon as you have a copy pattern. printf() and friends cannot be extended. You have a fixed list of format types.

C ++ I / O, while it looks pretty simple at the beginning, has a lot of structure available to programmers, so a good C ++ programmer can modify it to cover cases where CI / O cannot. (Do not abuse it.)

+11
source

The biggest gain is type safety. String strings in C are typeless (unlike, for example, OCaml or boost :: format), and so you can do pretty annoying things with them.

+4
source
  • You get an abstract input / output / search stream that can be implemented in any way.

    You write / read from a stream, which can be a file, memory, string, or even a special zlib filter or compressor!

    Some C libraries provide the ability to install read / write handlers (BSD and Linux), but still do not have the same power as std :: streambuf, and they are not standard.

  • You can use a stream-specific language that allows you to format data in accordance with any locale for a specific stream method.

  • Enter security.
  • Writing and reading from common stream objects (complex variables, XML object, etc.).

Further

+2
source

Firstly, if you use iostream, then you can work with std::string and not with char arrays, which means that you are much less concerned about memory.

+1
source

Well, if you use C ++, you can use OOP, right? I think cstdio (aka stdio.h) exists only for compatibility with C.

0
source

All Articles