I have been searching my texts, the web, and DaniWeb for information about how to share data between compiled C++ executables. I've been studying extern variables and named pipes without success. A typical program calls another program like this:

float Pi = 3.14;
system("AdjustSeaLevel.exe Pi 5.4 11.2 c f g");

and a typical receiving program looks like this:

int main(int argc, char* argv[])
{
float Pi;
// Confirm the args passed OK
   for(int i=0;i<argc;i++)
     cout << "Parameter argv[" << i << "]  = " << argv[i] << endl;

// Then convert literals (5.4, 11.2, etc.) to floats:
   float Celsius = atof(argv[2]);
   float Fahrenheit = atof(argv[3]);

// Then confirm literal successfully converted to float:
   Celsius = Celsius / 2; // etc.

// Or work with passed literals in other ways:
   if(argv[4][0] == 'c') cout << "Calculate in Celsius! " << endl; // etc.
}

And everything works fine, but the Pi variable passed by the calling program,

float Pi = 3.14;
system("AdjustSeaLevel.exe Pi 5.4 11.2 c f g");

arrives in called prog as a literal "Pi". I don't know how to substitute
the variable into the calling program's parameters.

One way around this of course is to have the calling prog store the needed piece of data in a disk file and then have the called prog open the file and retrieve the data, which in eff lets the disk function as a "pipe." But that requires a disk read and code that I would rather avoid if possible.

If someone knows a way to do this, I would like to know. Thanks, wiglaf.

whats your platform?

you can't concatenate it?

float Pi = 3.14;
system("AdjustSeaLevel.exe " + Pi + " 5.4 11.2 c f g");

or make a variable that contains the string to pass in, and then issue that?

#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>

using namespace std;

int main(int argc, char **argv)
{
	float Pi = 3.14;
	std::string tosend; 
	std::ostringstream buff;
	buff << Pi;

	tosend = buff.str() + " 5.4 11.2 c f g";
	cout << "something " + tosend;
	return 0;
}

you can't concatenate it?

float Pi = 3.14;
system("AdjustSeaLevel.exe " + Pi + " 5.4 11.2 c f g");

or make a variable that contains the string to pass in, and then issue that?

#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>

using namespace std;

int main(int argc, char **argv)
{
	float Pi = 3.14;
	std::string tosend; 
	std::ostringstream buff;
	buff << Pi;

	tosend = buff.str() + " 5.4 11.2 c f g";
	cout << "something " + tosend;
	return 0;
}

Comatose, thanks for your suggestions. I am using Windows XP, Borland Builder 4 and its built-in editor and compiler. I tried your first suggestion--with those headers,

system("AdjustSeaLevel.exe " + Pi + " 5.4 11.2 c f g");

which generated a floating point error, and then I tried your second group of code, which compiles OK, and I confirm in the calling program that the "tosend" string does correctly hold the desired floating poing Pi, and then I try to pass tosend to the receiving program with

system("AdjustSeaLevel.exe tosend");

but a literal "tosend" arrives in the called program (AdjustSeaLevel.exe), so what I'm missing is how to pass the variable to the called program. wiglaf

you can't put variables inside double quotes. That means it's a literal string then. You want the value of the variable, so you must take it out of the quotes..... You can't just take it out of quotes either, you must concatenate it.

system("AdjustSeaLevel.exe " + tosend);

you can't put variables inside double quotes. That means it's a literal string then. You want the value of the variable, so you must take it out of the quotes..... You can't just take it out of quotes either, you must concatenate it.

system("AdjustSeaLevel.exe " + tosend);

Comatose, that was my reasoning also, and your suggestion

system("AdjustSeaLevel.exe " + tosend;

was one of the first I tried. It generates these compiler errors:

Cannot convert 'string' to 'const char *' and
Type mismatch in parameter '__command' (wanted 'const char *', got 'string').

The entire program now looks like:

#include <conio.h>
#include <iostream.h>
#include <cstdio.h>
#include <cstring.h>
#include <sstream.h>
using namespace std;
#pragma argsused
int main(int argc, char* argv[])
{
float Pi = 3.14;
std::string tosend;
std::ostringstream buff;
buff << Pi;
tosend = buff.str() + " 5.4 11.2 c f g";
system("AdjustSeaLevel.exe " + tosend);
getch();
return 0;
}
I appreciate the time you've spent looking at this code. wiglaf

#include <sstream>
int main(int argc, char** argv){
//...
//your code here
//...

stringstream toPass;

toPass << "AdjustSealevel.exe" << Pi;

system(toPass.cstr());   //it might me c_str() instead of cstr() not too sure

EXIT_SUCCESS;
}

Yeah, it's .c_str()

>>Yeah, it's .c_str()
NO. it is .str() for string streams.It will return a string object on which you will have to do a .c_str(). So the corrected code would be somewhat like this:

#include<iostream>
#include<sstream>
int main()
{
    std::stringstream arg;
    const float pi=3.145745;
    arg<<"AdjustSealevel.exe "<<pi;

    std::cout<<arg.str().c_str();
    system(  arg.str().c_str()  );
}
commented: Aha, You Are Absolutely Right. Nice Job Man. +12

>>Yeah, it's .c_str()
NO. it is .str() for string streams.It will return a string object on which you will have to do a .c_str(). So the corrected code would be somewhat like this:

#include<iostream>
#include<sstream>
int main()
{
    std::stringstream arg;
    const float pi=3.145745;
    arg<<"AdjustSealevel.exe "<<pi;

    std::cout<<arg.str().c_str();
    system(  arg.str().c_str()  );
}

siddhant3s, your code works. I can now successfully pass the variable pi from a calling program, and it is received by the called program, and I verify it is a true float. I have also succ mixed literals with the passed string, as in

arg << "AdjustSealevel.exe c d e " << pi ;

and confirmed they are succ passed, but one task I have not solved is how to pass more than one variable, as in

float DO_Reading = 99.99;
arg << "AdjustSealevel.exe c d e " << pi + DO_Reading +
OtherVarsEtc ; // error

Can you tell me what mistake I am making? wiglaf

arg << "AdjustSealevel.exe c d e " << pi + DO_Reading +
OtherVarsEtc ;// is a error
int secondArgument=5;
std::string thirdArgument="somevalue";
arg<<"AdjustSealevel.exe "<<pi<<" "<<secondArgument<< " " << thirdArgument;

as you can infer, use << and not +

Its like this:
try to cout all the arguments and test as they will appear on screen.

cout<<"AdjustSealevel.exe "<<pi<<" "<<secondArgument<< " " << thirdArgument;

Now when you are confirmed that these are the EXACT argument you want to pass, just replace cout with arg.

The complete operative solution in code is available to anyone who wishes to contact wiglaf

I have tried this solution for passing data between 2 c++ executing programs, but everytime i send some data a new version of the other application is created. Correct data arrive but I end up with many instances of the same app. How to solve this. (Really new to C++ and this type of programming)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.