Passing data between C++ programs

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 7
Reputation: wiglaf is an unknown quantity at this point 
Solved Threads: 0
wiglaf wiglaf is offline Offline
Newbie Poster

Passing data between C++ programs

 
0
  #1
Mar 15th, 2009
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:
  1. int main(int argc, char* argv[])
  2. {
  3. float Pi;
  4. // Confirm the args passed OK
  5. for(int i=0;i<argc;i++)
  6. cout << "Parameter argv[" << i << "] = " << argv[i] << endl;
  7.  
  8. // Then convert literals (5.4, 11.2, etc.) to floats:
  9. float Celsius = atof(argv[2]);
  10. float Fahrenheit = atof(argv[3]);
  11.  
  12. // Then confirm literal successfully converted to float:
  13. Celsius = Celsius / 2; // etc.
  14.  
  15. // Or work with passed literals in other ways:
  16. if(argv[4][0] == 'c') cout << "Calculate in Celsius! " << endl; // etc.
  17. }
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.
Last edited by Ancient Dragon; Mar 15th, 2009 at 10:50 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: Passing data between C++ programs

 
0
  #2
Mar 15th, 2009
whats your platform?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Passing data between C++ programs

 
0
  #3
Mar 15th, 2009
you can't concatenate it?
  1. float Pi = 3.14;
  2. 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?
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char **argv)
  9. {
  10. float Pi = 3.14;
  11. std::string tosend;
  12. std::ostringstream buff;
  13. buff << Pi;
  14.  
  15. tosend = buff.str() + " 5.4 11.2 c f g";
  16. cout << "something " + tosend;
  17. return 0;
  18. }
Last edited by Comatose; Mar 15th, 2009 at 3:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: wiglaf is an unknown quantity at this point 
Solved Threads: 0
wiglaf wiglaf is offline Offline
Newbie Poster

Re: Passing data between C++ programs

 
0
  #4
Mar 15th, 2009
Originally Posted by Comatose View Post
you can't concatenate it?
  1. float Pi = 3.14;
  2. 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?
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char **argv)
  9. {
  10. float Pi = 3.14;
  11. std::string tosend;
  12. std::ostringstream buff;
  13. buff << Pi;
  14.  
  15. tosend = buff.str() + " 5.4 11.2 c f g";
  16. cout << "something " + tosend;
  17. return 0;
  18. }
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Passing data between C++ programs

 
0
  #5
Mar 15th, 2009
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.
  1. system("AdjustSeaLevel.exe " + tosend);
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: wiglaf is an unknown quantity at this point 
Solved Threads: 0
wiglaf wiglaf is offline Offline
Newbie Poster

Re: Passing data between C++ programs

 
0
  #6
Mar 15th, 2009
Originally Posted by Comatose View Post
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.
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 33
Reputation: Zcool31 is an unknown quantity at this point 
Solved Threads: 1
Zcool31 Zcool31 is offline Offline
Light Poster

Re: Passing data between C++ programs

 
0
  #7
Mar 15th, 2009
  1. #include <sstream>
  2. int main(int argc, char** argv){
  3. //...
  4. //your code here
  5. //...
  6.  
  7. stringstream toPass;
  8.  
  9. toPass << "AdjustSealevel.exe" << Pi;
  10.  
  11. system(toPass.cstr()); //it might me c_str() instead of cstr() not too sure
  12.  
  13. EXIT_SUCCESS;
  14. }
class NoClass {
	~NoClass () { delete this; }
};
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Passing data between C++ programs

 
0
  #8
Mar 15th, 2009
Yeah, it's .c_str()
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Passing data between C++ programs

 
1
  #9
Mar 16th, 2009
>>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:
  1. #include<iostream>
  2. #include<sstream>
  3. int main()
  4. {
  5. std::stringstream arg;
  6. const float pi=3.145745;
  7. arg<<"AdjustSealevel.exe "<<pi;
  8.  
  9. std::cout<<arg.str().c_str();
  10. system( arg.str().c_str() );
  11. }
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 7
Reputation: wiglaf is an unknown quantity at this point 
Solved Threads: 0
wiglaf wiglaf is offline Offline
Newbie Poster

Re: Passing data between C++ programs

 
0
  #10
Mar 16th, 2009
Originally Posted by siddhant3s View Post
>>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:
  1. #include<iostream>
  2. #include<sstream>
  3. int main()
  4. {
  5. std::stringstream arg;
  6. const float pi=3.145745;
  7. arg<<"AdjustSealevel.exe "<<pi;
  8.  
  9. std::cout<<arg.str().c_str();
  10. system( arg.str().c_str() );
  11. }
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC