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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:24:14+00:00 2026-05-27T08:24:14+00:00

Suppose I want to call a subprocess from within my program, and I want

  • 0

Suppose I want to call a subprocess from within my program, and I want to read the output from that subprocess into my program.

Here is a trivial way to do that:

//somefile.cpp
system("sub_process arg1 arg2 -o file.out");
           //call the subprocess and have it write to file
FILE *f = std::fopen("file.out", "r");
//.... and so on

We all know that i/o operations are computationally slow. To speed this up, I would like to skip the write-to-file-then-read-from-file step, and instead redirect the output of this sub-process directly into stdin (or some other stream)

How would I do this? How do I skip the i/o operation?

Note: many programs spit out some diagnostic stuff into stdout while they run, and write a clean version of the output to stdout (ex: stdout: “step1…done, step2…done, step3..done” -o file-out: “The magic number is: 47.28”), so ignoring the “-o ” argument and trusting that output will be automatically re-directed to stdout isn’t necessarily helpful…

Thanks to all in advance.

  • 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-27T08:24:14+00:00Added an answer on May 27, 2026 at 8:24 am

    Using popen skips the file, and gets you command’s output through an in-memory buffer.

    #include <iomanip>
    #include <iostream>
    using namespace std;
    const int MAX_BUFFER = 255;
    int main() {
        string stdout;
        char buffer[MAX_BUFFER];
        FILE *stream = popen("command", "r");
        while ( fgets(buffer, MAX_BUFFER, stream) != NULL )
            stdout.append(buffer);
        pclose(stream);
        cout << endl << "output: " << endl << stdout << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to put objects that identify a server into a stl set
Suppose I want to call one or more boost library functions from Ruby code.
Suppose I want to call a function f that asks for a function pointer
here is the situation: I want to call a method from a C++ module,
Is there any way to make a function call only once? Suppose I have
Suppose I have a function call from EF like: var result = context.myFunction(); the
Suppose I want to make an ajax call to the server and use the
Suppose I want a list of tuples. Here's my first idea: li = []
Suppose I want to have a method that passes the BeginInvoke method of an
Suppose I have a List[Int] , and I want to call toString on each

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.