Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3456920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:47:48+00:00 2026-05-18T09:47:48+00:00

I’ve never been much to use C++ I/O streams and have always opted for

  • 0

I’ve never been much to use C++ I/O streams and have always opted for what I know.
i.e. the printf functions.

I know there are some benefits to using I/O streams, but I’m looking for some tips
from the stackoverflow community to help me (or convince me) to switch. Because I still
prefer printf and I think the printf style is so much easier to read and quicker to type.

I would still like to be familiar with it even if I still continue to use printf.


Edit. Interestingly, google C++ coding style forbids the use of streams except for logging.
See: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Streams

Use streams only for logging. Definition: Streams are a
replacement for printf() and scanf().

Pros: With streams, you do not need to know the type of the object you
are printing. You do not have problems with format strings not
matching the argument list. (Though with gcc, you do not have that
problem with printf either.) Streams have automatic constructors and
destructors that open and close the relevant files.

Cons: Streams make it difficult to do functionality like pread(). Some
formatting (particularly the common format string idiom %.*s) is
difficult if not impossible to do efficiently using streams without
using printf-like hacks. Streams do not support operator reordering
(the %1s directive), which is helpful for internationalization.

Decision: Do not use streams, except where required by a logging
interface. Use printf-like routines instead.

There are various pros and cons to using streams, but in this case, as
in many other cases, consistency trumps the debate. Do not use streams
in your code.

Extended Discussion

There has been debate on this issue, so this explains the reasoning in
greater depth. Recall the Only One Way guiding principle: we want to
make sure that whenever we do a certain type of I/O, the code looks
the same in all those places. Because of this, we do not want to allow
users to decide between using streams or using printf plus
Read/Write/etc. Instead, we should settle on one or the other. We made
an exception for logging because it is a pretty specialized
application, and for historical reasons.

Proponents of streams have argued that streams are the obvious choice
of the two, but the issue is not actually so clear. For every
advantage of streams they point out, there is an equivalent
disadvantage. The biggest advantage is that you do not need to know
the type of the object to be printing. This is a fair point. But,
there is a downside: you can easily use the wrong type, and the
compiler will not warn you. It is easy to make this kind of mistake
without knowing when using streams.

cout << this;  // Prints the address 
cout << *this;  // Prints the contents 

The compiler does not generate an error because << has been
overloaded. We discourage overloading for just this reason.

Some say printf formatting is ugly and hard to read, but streams are
often no better. Consider the following two fragments, both with the
same typo. Which is easier to discover?

cerr << "Error connecting to '" << foo->bar()->hostname.first
     << ":" << foo->bar()->hostname.second << ": " << strerror(errno);
fprintf(stderr, "Error connecting to '%s:%u: %s",
      foo->bar()->hostname.first, foo->bar()->hostname.second,
      strerror(errno)); 

And so on and so forth for any issue you might bring up. (You could argue, “Things would
be better with the
right wrappers,” but if it is true for one scheme, is it not also true
for the other? Also, remember the goal is to make the language
smaller, not add yet more machinery that someone has to learn.)

Either path would yield different advantages and disadvantages, and
there is not a clearly superior solution. The simplicity doctrine
mandates we settle on one of them though, and the majority decision
was on printf + read/write.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:47:48+00:00Added an answer on May 18, 2026 at 9:47 am

    I’m not a big user of streams myself, so I’ll only list what I think about them. This is really subjective, I’ll understand if my answer is voted for deletion.

    • I like : homogeneity

    I may have a enum, a class or anything else, making my user defined type printable is always done by providing the same operator<< next to my type :

    std::ostream &operator<<(std::ostream &, const MyType &);
    

    You may ask yourself if a type is printable, but never how it is printable.

    • I like : abstraction

    Obviously, it is incredibly easy to provide ‘streaming capacities’ to a user defined type. It’s also a great to be able to provide our own implementation of a stream and have it fit transparently in an existing code. Once your operator<< are appropriately defined, writing to standard output, a memory buffer or a file is trivially changeable.

    • I dislike : formatting

    I’ve always thought iomanip to be a mess. I hate writing things such as (I’m just throwing random manipulators here) :

    std::cout << std::left << std::fixed << std::setprecision(0) << f << std::endl;
    

    I think it was much easier with printf, but Boost.Format is helpful here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.