C/C++: cout/ostream/istream vs (f)printf/scanf

  • Context: C/C++ 
  • Thread starter Thread starter neurocomp2003
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the advantages and disadvantages of using C++'s iostream (cout, ostream, istream) compared to C's printf and scanf for text input/output. Participants explore the implications of object-oriented programming, flexibility, and usability in different contexts.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants note that functionally, iostream and printf/scanf are similar, but iostream is more aligned with object-oriented programming.
  • Others argue that iostream methods provide greater flexibility and extensibility, particularly in handling user-defined types and ensuring type safety.
  • One participant expresses that many users may not utilize the advanced features of iostream, making the two methods effectively equivalent for their needs.
  • Concerns are raised about the difficulty of overloading printf to behave similarly to C++'s iostream, with some suggesting that it would require extensive overloads for various type combinations.
  • Another participant highlights the clarity of using iostream for formatting output compared to printf, citing the need to frequently reference man pages for C-style I/O formatting.
  • Some participants express a preference for printf due to familiarity and ease of formatting, while acknowledging the potential for more complex interactions with iostream.
  • One participant emphasizes the superiority of the ostream library in handling complex file loading and template interactions, while criticizing printf for lack of safety features like buffer overflow protection.

Areas of Agreement / Disagreement

Participants do not reach a consensus; multiple competing views remain regarding the advantages and usability of iostream versus printf/scanf.

Contextual Notes

Limitations include varying levels of familiarity with each method among participants, differing use cases, and the subjective nature of preferences for formatting and safety features.

neurocomp2003
Messages
1,359
Reaction score
4
is there any advantages to using one text I/O method over another
I'm more accustom to the prinft/scanf but i seem to see ostream/istream within C++ coding style( prolly because of the OO)

but is there really a difference?
 
Technology news on Phys.org
Functionally they aren't very different. count ,ostream, istream are just geared more toward OO programming like you mentioned.
 
The main difference between the two is in the flexiblity which the iostream methods give you over the C standard functions. ostream is more extensible, allowing you to easily handle (overload) your user defined types while also ensuring type safety. Basically, you can do more complex things more elegantly with ostream.
 
Sir_Deenicus, sure you can do all that, but for most people they don't use those capabilities so functionally to them it is basically the same.
 
you mean i can't overload the printf with a printf(Type& ) operator and use varg in the function?
 
Yeah, you can do that, but you can also overload << and >>.
 
yeah i know you can overload the <<, >>

was just curious at what Sir_Deenicus was indicating about the
extensibility...i just don't like <<,>> because i find it easier to format text
with printf. rather than setprecession(...) etc.
 
you mean i can't overload the printf with a printf(Type& ) operator and use varg in the function?
Not really. I assert it would be very difficult to create useful overloads printf so that it usually behaves similarly to that of the C library. The problem is that you'll have to overload it for every type combination that you'll ever expect to write.

Now, some libraries do provide mechanisms for introducing new types into printf's mechanism, and this should be doable. (But, of course, library specific)


One advantage that you should not ignore is this:

count << setprecision(5) << value << endl

is much clearer than

printf("%5f", value)

in fact, I suspect I have the printf version wrong; I have to hit the man pages any time I want to do something nontrivial with C-style i/o. At one time, it used to take quite a while to find the relevant entry, but I've had to do it often enough that I can usually remember at where and in what man page I need to look.
 
lol man pages...i hate linux/unix hehe...nah its %#.5f and I'm rather accustomed to using it especially doing things like %+-10.5f, +-10.5f I'm pretty sure you could probably right it into a string and send it into printf though, but I've seen some nice techniques if you know how to use varg vbegin.
 
  • #10
Well I had in mind where you hand to handle complex file loading and had to use such things as vector templates and probably templates in general and have them interact seamlessly with your objects. There, the ostream library is superior. Also I do not see why you would overload the printf -which you cannot do as an operator anyway- that is not a very good idea at all and is in principle not necessary. Also printf does not protect agaisnt buffer overflows and does not give you much safety or flexibility over strings...The list goes on.

Printf is just a quick and dirty way to get stuff out into output or vice versa with scanf.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
9K
  • · Replies 13 ·
Replies
13
Views
21K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 7 ·
Replies
7
Views
9K
  • · Replies 49 ·
2
Replies
49
Views
12K