Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does C++ iostreams have to offer in comparison with the C stdio library? [duplicate]

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.

like image 410
onemasse Avatar asked Dec 02 '22 04:12

onemasse


1 Answers

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.)

like image 84
David Thornley Avatar answered Dec 07 '22 23:12

David Thornley