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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:27:08+00:00 2026-06-18T23:27:08+00:00

In C++, I need to start a secondary program from a primary program, sending

  • 0

In C++, I need to start a secondary program from a primary program, sending the second some arguments. I need to return the data produced by the secondary program to the primary program. In this case, the data happens to be a two-dimensional std::string array; we’ll call it stringArray. This is easy enough to do:

// snippet from Primary
std::string executionString ("./secondaryProgram arg1 arg2 arg3");
system(executionString);

What I don’t know how to do is get the data that Secondary Program produces back to the Primary program (short of writing to a temporary file from Secondary and then reading the file from Primary).

In other words, it would be great if I could do something like:

// snippet from Primary
std::string stringArray[2][3];  
stringArray = system(executionString);

I’m not hoping for a solution as simple as this or working code from anyone, any nudge in the right direction is appreciated.

I cannot use sockets for this purpose. I have not been able to figure out how to build a pipe between std::cout and std::cin that works for this case. My only real constraint is that my solution involve system() somehow.

  • 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-18T23:27:09+00:00Added an answer on June 18, 2026 at 11:27 pm

    Alright here’s what I ended up doing.

    “translate”

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <sstream>
    #include <algorithm>
    #include <cstdlib>
    #include <unistd.h>
    
    std::vector<std::string> sortTerms(int n, char* argv[]) {
      std::vector<std::string> sortedTerms (n);
    
      for (int i = 0; i < n; i++) {
    sortedTerms[i] = argv[i+1]; // first term argv is program name
      }
    
      std::sort(sortedTerms.begin(),sortedTerms.end());
    
      return sortedTerms;
    }
    
    std::vector<std::string> splitString(int n,std::string str) {
      std::vector<std::string> stringVector (n);
    
      std::istringstream iss(str);
    
      for (int i = 0; i < n; i++) 
    std::getline(iss, stringVector[i], ' ');
    
      return stringVector;
    }
    
    int main(int argc, char** argv) {
      const int NUM_TERMS = (argc - 1); // number of words to translate
      std::string output[NUM_TERMS][2]; // used to store a translated word alongside the English equivalent
      std::string stringBuffer; // used to start dictionary with arguments
      std::vector<std::string> stringVector (NUM_TERMS); // used as a buffer
      std::ofstream outputFile("translated.txt"); // file to write translations to
      const bool VERBOSE = true;
    
      stringBuffer.clear();
      stringBuffer.append("./dictionary ");
    
      // Sort English words and load them into output
      stringVector = sortTerms(NUM_TERMS, argv);
      for (int i = 0; i < NUM_TERMS; i++) {
    output[i][0] = stringVector[i];
    stringBuffer = stringBuffer.append(stringVector[i]);
    stringBuffer = stringBuffer.append(" ");
      }
    
      int pipeStatus;
      int pipeOutput[2]; // file descriptor
    
      pipeStatus = pipe(pipeOutput); // create output read/write pipe ends
      if (pipeStatus < 0) {
    std::cerr << "ERROR CREATING PIPE" << std::endl;
    exit(1);
      }
    
      int pid = 0;
      pid = fork();
    
      if (pid == 0) { // dictionary
    // Connect the pipes
    dup2 (pipeOutput[1],1);
    
    // Execute the program
    system(stringBuffer.c_str());
    
    // Close pipes
    close(pipeOutput[0]);
    close(pipeOutput[1]);
    
    exit(0);
      }
      else if (pid > 0) { // Original process
    char* buffer = new char[1024]; // input buffer
    
    // Receive string from dictionary
    read(pipeOutput[0],buffer,1024); // read in from output of dictionary
    
    stringBuffer = buffer; // I'd rather work with a std::string
    
    stringVector = splitString(NUM_TERMS, stringBuffer);
    for (int i = 0; i < NUM_TERMS; i++)
      output[i][1] = stringVector[i];
    
    // Close pipes
    close(pipeOutput[0]);
    close(pipeOutput[1]);
    
    if (VERBOSE) {
      for (int i = 0; i < NUM_TERMS; i++)
        std::cout << output[i][0] << " -> " << output[i][1] << std::endl;
    }
    
    // write translationString to file
    for (int i = 0; i < NUM_TERMS; i++) {
      outputFile.write(output[i][0].c_str(),output[i][0].length());
      outputFile.write(" -> ",4);
      outputFile.write(output[i][1].c_str(),output[i][1].length());
      outputFile.write("\n",1);
    } 
    
    outputFile.close();
    
    exit(0);
      }
      else if (pid == -1) {
    std::cerr << "ERROR FORKING PROCESS" << std::endl;
    exit(1);
      }
      return 0;
    }
    

    “dictionary”

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <vector>
    
    std::vector<std::string> splitString(std::string str)
    {
      std::vector<std::string> stringVector (2);
    
      std::istringstream iss(str);
    
      std::getline(iss, stringVector[0], ' ');
      std::getline(iss, stringVector[1], ' ');
    
      return stringVector;
    }
    
    int main(int argc, char* argv[]) {  
      const int NUM_TERMS = (argc - 1);
      std::string stringBuffer;
      std::string returnString[NUM_TERMS];
      std::vector<std::string> stringVector;
      std::ifstream dictionaryFile ("./dictionary.txt");
    
      // There must be at least one arguement
      if (argc <= 1)
    std::cout << "Nothing to translate..." << std::endl;
    
      for (int i = 0; i < NUM_TERMS; i++) {
    while (dictionaryFile) {
      getline(dictionaryFile,stringBuffer);  
      stringVector = splitString(stringBuffer);
      if (stringVector[0] == argv[i+1]) { // wut
        returnString[i] = stringVector[1];
        break;
      }
    }
      }
    
      // clear string buffer
      stringBuffer.clear();
    
      // Form translated words string
      for (int i = 0; i < NUM_TERMS; i++) {
        stringBuffer.append(returnString[i]);
        if (i < (NUM_TERMS - 1))
            stringBuffer.append(" "); // append a space after each but the last term
      }
    
      // print translated words
      std::cout << stringBuffer << std::endl;
    
      dictionaryFile.close();
    
      return 0;
    }
    

    “dictionary.txt”

    Apple Apfel
    Banana Banane
    Blackberry Brombeere
    Blueberry Heidelbeere
    Cherry Kirsche
    Fruit Obst
    Grape Traube
    Lemon Zitrone
    Lime Limone
    Orange Orange
    Peach Pfirsich
    Pear Birne
    Plum Zwetschge
    Raspberry Himbeere
    Strawberry Erdbeere
    

    meant to be run like $ ./dictionary Apple Orange Strawberry

    produces “translated.txt”

    Apple -> Apfel
    Orange -> Orange
    Strawberry -> Erdbeere
    

    I’ve still got a bit of polishing to do before I turn it in, but that’s the gist of it. Thanks guys!

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

Sidebar

Related Questions

I need to start a copy of a Rails app from within Java. I
I need to start a program every time the user logs in, but I
I want to (need to) start a sub-process from a perl script that checks
I need a method to disable USB monitor (secondary monitor) from displaying especially during
Need some help on understanding how to do this; I'm going to be running
I need to make the primary .exe unrunnable from it (When you try to
I need start off with code because I am not sure what terminology to
I need to start up a jetty with an external (possibly remote) .war file
I need to start a Visual Basic script file by using WMI in a
I need to start a Unix process by calling a PHP-page through the web.

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.