Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Dec 17, 2005 #1
    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?
  2. jcsd
  3. Dec 17, 2005 #2


    User Avatar
    Staff Emeritus

    Functionally they aren't very different. cout ,ostream, istream are just geared more toward OO programming like you mentioned.
  4. Dec 18, 2005 #3
    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.
  5. Dec 18, 2005 #4


    User Avatar
    Staff Emeritus

    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.
  6. Dec 18, 2005 #5
    you mean i can't overload the printf with a printf(Type& ) operator and use varg in the function?
  7. Dec 18, 2005 #6


    User Avatar
    Staff Emeritus

    Yeah, you can do that, but you can also overload << and >>.
  8. Dec 18, 2005 #7
    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.
  9. Dec 18, 2005 #8


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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:

    cout << 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.
  10. Dec 18, 2005 #9
    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.
  11. Dec 19, 2005 #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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?