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 456,499 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 2,723 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: Programming Forums

passing data to a program

Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
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 Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
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;
}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
All times are GMT -4. The time now is 3:29 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC