User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,083 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,962 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1107 | Replies: 4
Reply
Join Date: Jul 2006
Posts: 4
Reputation: geek is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
geek geek is offline Offline
Newbie Poster

passing data to a program

  #1  
Dec 31st, 2006
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: passing data to a program

  #2  
Dec 31st, 2006
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.
// Program A
int main(int argc, char **argv)
{
  int n1, n2;
   // This program expects to get two numbers in argv.  
   // So the value of argc must be 3.
   if( argc != 3)
   {
     printf("wrong number of arguments\n");
     return 1;
   }
   n1 = atoi(argv[1]);
   n2 = atoi(argv[2]);

   // now do something with the two numbers not shown



   // exit the program
   return 0;
}

This is program 2
int main()
{
   // send program a.exe two numbers -- 12 and 13
    system("a.exe 12 13");

    return 0;
}


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.
// program b.exe
int main()
{
     fstream out(myfile.txt);
    out << "12\n13\n");
    fclose(out);
    system("a.exe <myfile.txt");

   return 0;
}

There are other solutions as well, but I think the above are probably the easiest to implement.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jul 2006
Posts: 4
Reputation: geek is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
geek geek is offline Offline
Newbie Poster

Re: passing data to a program

  #3  
Jan 1st, 2007
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 2:06 pm.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,775
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: passing data to a program

  #4  
Jan 1st, 2007
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:
  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:
  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.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: passing data to a program

  #5  
Jan 1st, 2007
Originally Posted by geek View Post
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
// program A 
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	int a,b;
	cout << "Enter first number\n";
	cin >> a;
	cin.ignore();
	cout << "Enter second number\n";
	cin >> b;
	cin.ignore();
	cout << "a: " << a << " b: " << b << "\n";
	return 0;
}
// Program B.  This will send two numbers, 12 and 13, to program A.
#include <stdio.h>
#include <stdlib.h>



int main(int argc, char* argv[])
{

	char *cmd = "..\\progA\\debug\\progA.exe";
	FILE *ptr;
	if ((ptr = _popen(cmd, "w")) != NULL)
	{
		fprintf(ptr, "%d\n%d\n", 12, 13);
		_pclose(ptr);
	}
	return 0;

	return 0;
}
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:25 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC