1

C ++では、プライマリプログラムからセカンダリプログラムを起動し、2番目にいくつかの引数を送信する必要があります。二次プログラムによって生成されたデータを一次プログラムに戻す必要があります。この場合、データはたまたま2次元のstd::string配列です。それを呼びますstringArray。これは簡単に実行できます。

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

方法がわからないのは、セカンダリプログラムが生成したデータをプライマリプログラムに戻すことです(セカンダリから一時ファイルに書き込んでからプライマリからファイルを読み取ることはできません)。

言い換えれば、私が次のようなことをすることができれば素晴らしいでしょう:

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

私はこれほど単純な解決策や誰かからの実用的なコードを望んでいません。正しい方向に少しでも進んでいただければ幸いです。

この目的でソケットを使用することはできません。この場合に機能するstd::coutとstd::cinの間にパイプを構築する方法を理解できませんでした。私の唯一の本当の制約は、私の解決策がsystem()何らかの形で関係しているということです。

4

6 に答える 6

0

system()子プロセスへのパイプは作成されません。子プロセスは、親の標準入力、標準出力、および標準エラー記述子を継承します。

Linuxではpopen()、子のstdinまたはstdoutにアクセスする場合に使用できます。

を使用するsystem()必要があるため、セカンダリプログラムにその結果をファイルに保存させることができます。システムが完了した後、メインプログラムはファイルを開きます。このようなもの:

std::string executionString ("./secondaryProgram arg1 arg2 arg3 > output_file.txt");
system(executionString);
std::ifstream result("output_file.txt");
while( result >> str) {
  result_vector.push_back(str);
}
于 2013-02-12T19:10:59.643 に答える
0

boost.interprocessを見てください。ポータブルな方法でIPCに使用できる多くのユーティリティが含まれています。

ブーストに依存したくない場合は、次のようにすることができます。C++11モードと-pthreadGCCオプションを使用してコンパイルします。

于 2013-02-12T19:38:15.943 に答える
0

2番目のプロセスで関連情報をファイルに書き込んでから、最初のプロセスでそのファイルを読み取ってみませんか。このようにするのは奇妙に思えますが、少なくともあなたが私たちと共有した部分は、あなたの教授の基準を満たしていると思います。

于 2013-02-12T20:42:54.273 に答える
0

BoostInterprocessが機能するはずです。異なるプロセス上のスレッド間のメッセージキューをサポートします。

于 2013-02-12T20:50:53.777 に答える
0

パイプを使用して通信できます。提供されているリンクにはLinuxの例がありますが、Windows用に作成するものと非常によく似ています。

実行時に変更される可能性のある任意のデータを送信する必要がある場合は、パイプを介して送信されたデータをシリアル化し、受信側で逆シリアル化することを検討してください。XML、JSON、またはProtobufのようなものを使用できます。人間が読める形式にすると、コンポーネントを再利用したり、眼球マーク1を使用して何が起こっているかをデバッグしたりする機会が追加されます。

于 2013-02-12T21:05:00.210 に答える
0

さて、これが私がやったことです。

"翻訳"

#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;
}

"辞書"

#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

次のように実行することを意味します$ ./dictionary Apple Orange Strawberry

「translated.txt」を生成します

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

提出する前に、まだ少し磨きをかける必要がありますが、それが要点です。みんなありがとう!

于 2013-02-18T02:20:00.233 に答える