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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:46:54+00:00 2026-06-18T09:46:54+00:00

It is well known that there are three default I/O streams which are mapped

  • 0

It is well known that there are three default I/O streams which are mapped to predefined objects in the standard library:

  • 0: std::istream std::cin
  • 1: std::ostream std::cout
  • 2: std::ostream std::cerr and std::ostream std::clog

However, from within (e.g.) a bash script you can create additional streams (3,4,…).

So, can you create an extra output stream with the descriptor 3 and bind it to an std::ostream custom object? If so, how? std::ofstream doesn’t do the trick as it would create a file with the name “3” which is not what I want.


Edit: It doesn’t have to be portable. It suffices if it works on POSIX.

  • 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-18T09:46:56+00:00Added an answer on June 18, 2026 at 9:46 am

    It is not possible if you need your program to be portable. The C++ 11 Standard does not specify a unified way of doing that.

    However, you can define your own output stream buffer which overrides the overflow() and xsputn() virtual functions and writes each character or sequence of characters to the stream with the specified descriptor using system-specific API.

    Something along these lines:

    class my_ostream_buf : public std::streambuf
    {
    
    public:
    
        my_ostream_buf(int fd) : _fd(fd) { }
    
    protected:
    
        virtual int_type overflow (int_type c)
        {
            if (c != EOF)
            {
                char ch = c;
                if (write(_fd, &ch, 1) != 1)
                {
                    return EOF;
                }
            }
    
            return c;
        }
    
        // This is not strictly necessary, but performance is better if you
        // write a sequence of characters all at once rather than writing
        // each individual character through a separate system call.
        virtual std::streamsize xsputn(const char* s, std::streamsize num)
        {
            return write(_fd, s, num);
        }
    
    private:
    
        int _fd = 0;    
    
    };
    

    And this is how you would use it:

    using namespace std;
    
    int main()
    {
        int fd = ...; // Any file descriptor
        my_ostream_buf buf(fd);
    
        ostream os(&buf); // Take care of the lifetime of `buf` here, or create your
                          // own class that derives from ostream and encapsulates an
                          // object of type my_ostream_buf
    
        os << "Hello" << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's pretty well known that the default behaviour of std::bind and std::thread is that
It's well known that emacs can be used as a terminal emulator (while itself
In C#, it is well known that ..CompareTo(A) == -1 . My question is:
I can't imagine that there doesn't exist an efficient, lightweight, secure authentication and authorization
There are some parts of the framework which are not quite clear to me
I know that default WPF behavior is to render WPF controls and then on
I have a WebForm application that generates a Crystal Report and streams it to
I have a project (a library) that is subdivided into a few directories with
The fact that Haskell's default String implementation is not efficient both in terms of
I searched SO and found similar questions, but none compared all three. That surprised

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.