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

The Archive Base Latest Questions

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

I need to read the output from another program from my program. To do

  • 0

I need to read the output from another program from my program. To do that, I can use popen(). This way, I get a FILE* to the program’s output that can be parsed in my program. How can I read and parse the content of the return value of popen() from my program? Alternatively, is there a more C++-like version of popen somewhere?

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

    You can do either create a stream buffer doing the moral equivalent of popen() or you can create a stream buffer wrapping FILE*. Once you got a stream buffer, you can use it with std::istream which seems to be what you intend to do. A simple (and untested) stream buffer reading from a FILE* would look something like this:

    struct FILEbuf
        : std::streambuf {
        FILEbuf(FILE* fp): fp_(fp) {}
        int underflow() {
            if (this->gptr() == this->egptr()) {
                int size = fread(this->buffer_, 1, s_size, this->fp_);
                if (0 < size) {
                    this->setg(this->buffer_, this->buffer_, this->buffer_ + size);
                }
            }
            return this->gptr() == this->egptr()
                ? traits_type::eof()
                : traits_type::to_int_type(*gptr());
         }
         FILE* fp_;
         enum { s_size = 1024 };
         char  buffer_[s_size];
    };
    

    I reckon I mistyped something but roughly the idea does work: Just read from the FILE* into a stream buffer. Alternatively, create a stream reading from a spawned process. This take a couple of platform specific calls to fork(), exec..(), pipe(), close(), dup2() (probably forgot something here, too) but can be done as well.

    A stream buffer like the one above could be used, e.g., like this:

    int main()
    {
        FILEbuf      sbuf(fopen("pstream.cpp", "r"));
        std::istream in(&sbuf);
        for (std::string line; std::getline(in, line); ) {
            std::cout << line << '\n';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way that I can run a script from another while getting
I need to read the output from ffmpeg in order to even try the
I need to read 16 bits from the binary file as std::string or char
I need to read data from XML to a List<>. The XML file contains
I need to read the editurl from one jqgrid for use in a DND
I need to read in an expression from a file using a string stream
I'm writing a script that logs errors from another program and restarts the program
Basically I need to open and read a list of files I get from
The goal is to periodically read files from a folder to which another program
In another question here I've read that python-magic should output the correct mime type

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.