在两个独立的程序之间传递信息 [英] Passing information between two seperate programs

查看:282
本文介绍了在两个独立的程序之间传递信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的程序中传递一个输入变量的值,让#1到另一个程序#2,我想要#2打印它到屏幕的数据,都需要用c ++编写。

I want to pass a value of an input variable in my program lets say#1 to another program #2 and i want #2 to print the data it got to screen, both are needed to be written in c++. The this will be on Linux.

推荐答案

为了回应你对Roopesh Majeti的回答,这里有一个非常简单的例子使用环境变量:

In response to your comment to Roopesh Majeti's answer, here's a very simple example using environment variables:

第一个程式:

// p1.cpp - set the variable
#include <cstdlib>
using namespace std;;    
int main() {
    _putenv( "MYVAR=foobar" );
    system( "p2.exe" );
}

第二个程序:

// p2.cpp - read the variable
#include <cstdlib>
#include <iostream>
using namespace std;;

int main() {
    char * p = getenv( "MYVAR" );
    if ( p == 0 ) {
    	cout << "Not set" << endl;
    }
    else {
    	cout << "Value: " << p << endl;
    }
}

注意:


  • 没有标准的设置环境变量的方法

  • 您将需要从变量contents

  • there is no standard way of setting an environment variable
  • you will need to construct the name=value string from the variable contents

这篇关于在两个独立的程序之间传递信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆