943,940 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2274
  • C++ RSS
Dec 31st, 2006
-1

passing data to a program

Expand Post »
Hello everybody,

I have a program in c++ (console) that takes in numbers...I want to write another program to open this program and send numbers to it automatically...instead of me typing this numbers and pressing enter....how can I do this??

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek is offline Offline
4 posts
since Jul 2006
Jan 1st, 2007
0

Re: passing data to a program

There are a couple ways you can do it.
1. The program that is to get the numbers should get then from argv. The program that passes the numbers just passes them in the argument list as if you passed them on the command line.
C++ Syntax (Toggle Plain Text)
  1. // Program A
  2. int main(int argc, char **argv)
  3. {
  4. int n1, n2;
  5. // This program expects to get two numbers in argv.
  6. // So the value of argc must be 3.
  7. if( argc != 3)
  8. {
  9. printf("wrong number of arguments\n");
  10. return 1;
  11. }
  12. n1 = atoi(argv[1]);
  13. n2 = atoi(argv[2]);
  14.  
  15. // now do something with the two numbers not shown
  16.  
  17.  
  18.  
  19. // exit the program
  20. return 0;
  21. }

This is program 2
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. // send program a.exe two numbers -- 12 and 13
  4. system("a.exe 12 13");
  5.  
  6. return 0;
  7. }


2. Another method if program a.exe is already written to get the two numbers from the keyboard then program b should write the numbers to a file and redirect the input for program a to the file.
C++ Syntax (Toggle Plain Text)
  1. // program b.exe
  2. int main()
  3. {
  4. fstream out(myfile.txt);
  5. out << "12\n13\n");
  6. fclose(out);
  7. system("a.exe <myfile.txt");
  8.  
  9. return 0;
  10. }

There are other solutions as well, but I think the above are probably the easiest to implement.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 1st, 2007
0

Re: passing data to a program

Thanks for responding Ancient Dragon, I try doing that but for some reason it does not send the data......numbers to the other program...it does open up the other program usgin the system"app.exe 12 13"); but the program does not receive the inputs....12 13 ...the cursor just blinks......

I am not using args in the first program...just "cin>>" is there a way to still do this...withouth the args?
Last edited by geek; Jan 1st, 2007 at 3:06 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geek is offline Offline
4 posts
since Jul 2006
Jan 1st, 2007
0

Re: passing data to a program

I don't think there is any other way than command line arguments since the ways in which a program receives data are:

1. Standard input (keyboard in most cases)
2. File stream (reading data from files)
3. Command line arguments

Since you don't require 1 and 2, you obviously have to go with the third choice, that is command line arguments.

Now just a simple test:

1. Create a project "Driver" which willl contain a single file driver.cpp with contents:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(int argc, char **argv)
  5. {
  6. int n1, n2;
  7. // This program expects to get two numbers in argv.
  8. // So the value of argc must be 3.
  9. if( argc != 3)
  10. {
  11. printf("wrong number of arguments\n");
  12. return 1;
  13. }
  14. n1 = atoi(argv[1]);
  15. n2 = atoi(argv[2]);
  16.  
  17. cout << "The output in the driver program is " << n1 + n2 ;
  18.  
  19. return 0;
  20. }

2. Compile the project so that the executable is created (driver.exe)

3. Create a project "Main" which will contain a single file main.cpp with the following contents:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. system("driver 12 13") ;
  6. return 0;
  7. }

4. Make sure the project "Main" and "Driver" are in the same directory (just for our convinience) along with their executables.

5. Compile and Run the main.cpp and you will see the expected output. Try this and see if it works in your case.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 1st, 2007
0

Re: passing data to a program

Click to Expand / Collapse  Quote originally posted by geek ...
I am not using args in the first program...just "cin>>" is there a way to still do this...withouth the args?
See my second example. put the numbers in a text file then redirect stdin from that file.

Another method is to use pipes. Here is an example program. The example opens the pipe for reading -- but in your case you will want to open it for writing because you want to send the other program the numbers.

Here is a working example for MS-Windows os
C++ Syntax (Toggle Plain Text)
  1. // program A
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7. int a,b;
  8. cout << "Enter first number\n";
  9. cin >> a;
  10. cin.ignore();
  11. cout << "Enter second number\n";
  12. cin >> b;
  13. cin.ignore();
  14. cout << "a: " << a << " b: " << b << "\n";
  15. return 0;
  16. }
C++ Syntax (Toggle Plain Text)
  1. // Program B. This will send two numbers, 12 and 13, to program A.
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.  
  10. char *cmd = "..\\progA\\debug\\progA.exe";
  11. FILE *ptr;
  12. if ((ptr = _popen(cmd, "w")) != NULL)
  13. {
  14. fprintf(ptr, "%d\n%d\n", 12, 13);
  15. _pclose(ptr);
  16. }
  17. return 0;
  18.  
  19. return 0;
  20. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

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: Problem to write C program
Next Thread in C++ Forum Timeline: help for error





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


Follow us on Twitter


© 2011 DaniWeb® LLC