943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3700
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 15th, 2009
0

Passing data between C++ programs

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wiglaf is offline Offline
7 posts
since Apr 2008
Mar 15th, 2009
0

Re: Passing data between C++ programs

whats your platform?
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Mar 15th, 2009
0

Re: Passing data between C++ programs

you can't concatenate it?
C++ Syntax (Toggle Plain Text)
  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?
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 15th, 2009
0

Re: Passing data between C++ programs

Click to Expand / Collapse  Quote originally posted by Comatose ...
you can't concatenate it?
C++ Syntax (Toggle Plain Text)
  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?
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wiglaf is offline Offline
7 posts
since Apr 2008
Mar 15th, 2009
0

Re: Passing data between C++ programs

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.
C++ Syntax (Toggle Plain Text)
  1. system("AdjustSeaLevel.exe " + tosend);
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 15th, 2009
0

Re: Passing data between C++ programs

Click to Expand / Collapse  Quote originally posted by Comatose ...
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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wiglaf is offline Offline
7 posts
since Apr 2008
Mar 15th, 2009
0

Re: Passing data between C++ programs

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Zcool31 is offline Offline
54 posts
since Mar 2009
Mar 15th, 2009
0

Re: Passing data between C++ programs

Yeah, it's .c_str()
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 16th, 2009
1

Re: Passing data between C++ programs

>>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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Mar 16th, 2009
0

Re: Passing data between C++ programs

Click to Expand / Collapse  Quote originally posted by siddhant3s ...
>>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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wiglaf is offline Offline
7 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: pthread lock hangs
Next Thread in C++ Forum Timeline: C++ Address Book logical errors...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC