我需要在execl中传递两个整数值,我将它们转换为(const char *),然后传递它们。在execl中,我想将参数传递给其他cpp文件,然后这些程序将计算,然后它们将返回结果。在execl中的路径(linux)
我认为(add/min/mul/div)程序在execl中没有正确调用。
static int sum_calculation(tree *&root , tree *&root2, int value1 , int value2 , int result_tree)
{
if(root->l_child == NULL && root->r_child == NULL)
return root->data;
else
{
value1 = sum_calculation(root->l_child , root2 , value1 , value2 , result_tree);
value2 = sum_calculation(root->r_child , root2 , value1 , value2 , result_tree);
stringstream str1 , str2;
str1 << value1;
str2 << value2;
string temp_str1 = str1.str();
string temp_str2 = str2.str();
const char* ch1 = (char*) temp_str1.c_str();
const char* ch2 = (char*) temp_str2.c_str();
int fd2[2];
pipe(fd2);
if(fork() == 0)
{
const char *adder = "add";
const char *multiplier = "mul";
const char *subtractor = "min";
const char *divider = "div";
if(root->data == 43)
execl(adder , ch1 , ch2);
else if(root->data == 45)
execl(subtractor , ch1 , ch2);
else if(root->data == 42)
execl(multiplier , ch1 , ch2);
else if(root->data == 47)
execl(divider , ch1 , ch2);
close(fd2[0]);
write(fd2[1] , &result_tree , sizeof(result_tree));
exit(0);
}
else
{
close(fd2[1]);
read(fd2[0] , &result_tree , sizeof(result_tree));
//wait();
}
root->data = result_tree;
delete_node(root2 , root);
return result_tree;
}
}
添加的功能是:
#include <sstream>
#include <string.h>
#include<iostream>
using namespace std;
int main(int argc , char *argv[])
{
int Result , value1 , value2;
stringstream convert1(argv[1]);
stringstream convert2(argv[2]);
if (!(convert1 >> value1))
Result = 0;
if (!(convert2 >> value2))
Result = 0;
Result = value1 + value2;
return Result;
}
分钟(subtratction)/ MUL/DIV CPP的类似于添加 三江源的帮助。
我包括管道,因为我需要管道,但它还没有,但我必须包括它,然后我会尽量修剪问题。 Thankyou的帮助 – user2831683
@ user2831683,再次阅读我的答案。我不知道(或者很在乎)你想做什么;从问题的核心中分心越少,对于我们(或您,就此而言)越容易查看它出错的地方。 – vonbrand