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 1053041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:11:24+00:00 2026-05-16T17:11:24+00:00

Comments on my answer here have made me think about how one might implement

  • 0

Comments on my answer here have made me think about how one might implement the same pattern I’ve been doing with C++ streams. Specifically, I need to be able to have a function which can write to the console, a file, or a string/memory buffer. I don’t need most of the formatting features and such that IOStreams provide. Is there some better design which would allow this type of redirection idiomatically that’s commonly used in other circles?

for example – for testing I need to be able to write to a string, but for real program use it’s always going to be going to a file or the console.

  • 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-16T17:11:25+00:00Added an answer on May 16, 2026 at 5:11 pm

    Totally untested, but you get the idea.

    struct stdio_stream {
        enum { invalid_t, file_t, str_t } which;
        union {
            FILE *file_p;
            string *str_p;
        };
    
        int printf( char *fmt, ... );
        int scanf( char *fmt, ... );
    
        stdio_stream() : which( invalid_t ), file_p( NULL ) {};
    
         // etc
    };
    
    int stdio_stream::printf( char *fmt, ... ) {
        int ret;
        va_list args;
        va_start( args, fmt );
    
        if ( which == file_t ) {
            ret = vfprintf( file_p, fmt, args );
        } else if ( which == str_t ) {
            string_p->resize( vsnprintf( NULL, 0, fmt, args )+1, '\0' );
            ret = vsnprintf( &* string_p->begin(), string_p->size(), fmt, args );
        } else throw runtime_error( "uninitialized stream" );
    
        va_end( args );
        return ret;
    }
    

    Personally, I think iostream is just great. If I need to go fast, I’ll write a fast parser. printf still has a lot of room for speed improvement… it’s a nice middle ground, I suppose, but still just a compromise.

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

Sidebar

Related Questions

In the comments on this answer , Remou writes that CurrentDB.Execute insert sql here
I have a simple question and answer section on my website that allows comments.
I'm really sorry, I realize there have been several questions asked about cocos2d touch
I know this has been asked before but I have looked at every answer
As a followon to the discussion in the comments of this answer , should
This question arose from the discussion in the comments of this answer . First,
I have these tables: comments uid id pid pages pid user users id rank
I have Comments for something and it's possible to like them. The likes are
I have included Django comments framework in my project, and added custom templates to
I'm sure there's an easy answer here and i'm just over-complicating things, but i

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.