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

  • SEARCH
  • Home
  • 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 8475621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:56:33+00:00 2026-06-10T17:56:33+00:00

In c++, Is it possible to write to some kind of buffer with printf

  • 0

In c++, Is it possible to write to some kind of buffer with printf (or similar) and then later in the program either write the buffer to the screen or discard it depending on the outcome.

I want to do this because I have a recursive function and only want the see the things printed throughout the recursion if the result is of interest.

  • 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-06-10T17:56:34+00:00Added an answer on June 10, 2026 at 5:56 pm

    Certainly. You can leverage the power of vsnprintf for that purpose. I’d suggest some sort of class wrapping an std::string or std::vector<char> (essentially the same in C++11):

    #include <cstdargs>
    #include <cstdio>
    #include <string>
    
    class Formatter
    {
        std::string buf;
    public:
        void add(char const * fmt, ...)
        {
            std::va_list ap, aq;
            va_start(ap, fmt);
            va_copy(aq, ap);
    
            int ret1 = std::vsnprintf(NULL, 0, fmt, ap);
            // check ret1 != -1
    
            std::size_t cur = buf.size();
            buf.resize(cur + ret1 + 1);
    
            int ret2 = std::vsnprintf(&buf[cur], ret1 + 1, fmt, aq);
            // check ret2 != -1
            buf.resize(cur + ret1);
    
            va_end(aq);
            va_end(ap);
        }
    
        std::string const & str() const { return buf; }
    };
    

    Now you can say:

    Formatter f;
    f.add("Hello, %s", "world");
    f.add("%i%i%i", 1, 2, 3);
    std::cout << f.str() << std::endl;
    

    If you’re very concerned about performance, you can try and preallocate some space for the print operation and maintain a separate “end” position, in the hope that you’ll never have to run the vnsprintf call more than once.

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

Sidebar

Related Questions

A customer (photographer) asked me, if it was possible to write some kind of
Is it possible to write and run with Visual Studio 2008 some unit test
I am trying to write a query and would like some help if possible.
Is it possible to write a program to do things you constantly do on
This is some kind of academic question, I just wondering if it is possible
Is is possible (in Ruby or in general) to create some kind of blackhole
I'm trying to write a matlab code to generate some kind of result page
Possible Duplicate: Why do some experienced programmers write comparisons with the value before the
I wrote program in java which is using some kind of win lib and
Is possible to write language agnostic Robotium tests? For example, if you use a

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.