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

The Archive Base Latest Questions

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

Using Linux and C++, I would like a function that does the following: string

  • 0

Using Linux and C++, I would like a function that does the following:

string f(string s)
{
    string r = system("foo < s");
    return r;
}

Obviously the above doesn’t work, but you get the idea. I have a string s that I would like to pass as the standard input of a child process execution of application "foo", and then I would like to record its standard output to string r and then return it.

What combination of Linux syscalls or POSIX functions should I use?
I’m using Linux 3.0 and do not need the solution to work with older systems.

  • 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-12T16:46:16+00:00Added an answer on June 12, 2026 at 4:46 pm

    The code provided by eerpini does not work as written. Note, for example, that the pipe ends that are closed in the parent are used afterwards. Look at

    close(wpipefd[1]); 
    

    and the subsequent write to that closed descriptor. This is just transposition, but it shows this code has never been used. Below is a version that I have tested. Unfortunately, I changed the code style, so this was not accepted as an edit of eerpini’s code.

    The only structural change is that I only redirect the I/O in the child (note the dup2 calls are only in the child path.) This is very important, because otherwise the parent’s I/O gets messed up. Thanks to eerpini for the initial answer, which I used in developing this one.

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    
    #define PIPE_READ 0
    #define PIPE_WRITE 1
    
    int createChild(const char* szCommand, char* const aArguments[], char* const aEnvironment[], const char* szMessage) {
      int aStdinPipe[2];
      int aStdoutPipe[2];
      int nChild;
      char nChar;
      int nResult;
    
      if (pipe(aStdinPipe) < 0) {
        perror("allocating pipe for child input redirect");
        return -1;
      }
      if (pipe(aStdoutPipe) < 0) {
        close(aStdinPipe[PIPE_READ]);
        close(aStdinPipe[PIPE_WRITE]);
        perror("allocating pipe for child output redirect");
        return -1;
      }
    
      nChild = fork();
      if (0 == nChild) {
        // child continues here
    
        // redirect stdin
        if (dup2(aStdinPipe[PIPE_READ], STDIN_FILENO) == -1) {
          exit(errno);
        }
    
        // redirect stdout
        if (dup2(aStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1) {
          exit(errno);
        }
    
        // redirect stderr
        if (dup2(aStdoutPipe[PIPE_WRITE], STDERR_FILENO) == -1) {
          exit(errno);
        }
    
        // all these are for use by parent only
        close(aStdinPipe[PIPE_READ]);
        close(aStdinPipe[PIPE_WRITE]);
        close(aStdoutPipe[PIPE_READ]);
        close(aStdoutPipe[PIPE_WRITE]); 
    
        // run child process image
        // replace this with any exec* function find easier to use ("man exec")
        nResult = execve(szCommand, aArguments, aEnvironment);
    
        // if we get here at all, an error occurred, but we are in the child
        // process, so just exit
        exit(nResult);
      } else if (nChild > 0) {
        // parent continues here
    
        // close unused file descriptors, these are for child only
        close(aStdinPipe[PIPE_READ]);
        close(aStdoutPipe[PIPE_WRITE]); 
    
        // Include error check here
        if (NULL != szMessage) {
          write(aStdinPipe[PIPE_WRITE], szMessage, strlen(szMessage));
        }
    
        // Just a char by char read here, you can change it accordingly
        while (read(aStdoutPipe[PIPE_READ], &nChar, 1) == 1) {
          write(STDOUT_FILENO, &nChar, 1);
        }
    
        // done with these in this example program, you would normally keep these
        // open of course as long as you want to talk to the child
        close(aStdinPipe[PIPE_WRITE]);
        close(aStdoutPipe[PIPE_READ]);
      } else {
        // failed to create child
        close(aStdinPipe[PIPE_READ]);
        close(aStdinPipe[PIPE_WRITE]);
        close(aStdoutPipe[PIPE_READ]);
        close(aStdoutPipe[PIPE_WRITE]);
      }
      return nChild;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using linux. I have a function called like: PlayBackgroundIntroMusic((char *)IntroMusic); The functions is:
Using C on Linux, how would I go about triggering a signal handler every
I would like to have a small application loader program that receives other binary
I like a lot of what I've read about D. Unified Documentation (That would
Using File::Find , how can I pass parameters to the function that processes each
I have the following example code and would like to know what kind of
My co-workers would like to make sure that our work in R is platform-independent,
Using Linux environment with java,I'm having the config file which should be configured before
im using linux with gedit which has the wonderful habit of creating a temp
I am using Linux Mint 12, Kernel 3. I have installed and configured Open

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.