srik27
asked on
popen function.......
Hi,
Read the following 2 programs and read the question below....
this is popentest2.cpp
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define BUFSIZE 100
void main()
{
char* i="hello";
char buf[BUFSIZE];
FILE *fp;
fp = _popen("path of popentest1.exe","r+");
if(fp==NULL)
printf(" error reading file");
while (fgets(buf, BUFSIZ, fp) != NULL)
(void) printf("%s", buf);
fputs("5",fp);
getch();
}
this is popentest1.cpp.....
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(" this is testing.... ");
cin>>a;
printf("%d",2*a);// this shud return 10....2*5
}
2. run popentest1.cpp initially to create the exe file. after that stop it.
1. run popentest2.cpp
now i want to read what was written in the stream of popentest1.exe and write into the stream of popentest1.exe...that is to be able to read from it and to supply arguments to popentest1.exe.
However i want it to be done without pressing enter. when we use cin, the prompt is waiting for enter to be pressed, plz help me, i want it automated that is after some parameter is written to popentest1.exe it should continue without waiting for enter to be pressed.
i dont mind using any other function but i need a solution to this. Please kindly help. if i pass '\n' as parameter also it is not working properly i think.
or to put it simply...
just give me 2 programs, i should be able to start one program from the other read from that program and write to it...using popen or anything else in vc++...just supplying the parameters and the programs shud not wait for "enter" . Even if u send '\n' parameter also its fine.
plz help.
S.
Read the following 2 programs and read the question below....
this is popentest2.cpp
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define BUFSIZE 100
void main()
{
char* i="hello";
char buf[BUFSIZE];
FILE *fp;
fp = _popen("path of popentest1.exe","r+");
if(fp==NULL)
printf(" error reading file");
while (fgets(buf, BUFSIZ, fp) != NULL)
(void) printf("%s", buf);
fputs("5",fp);
getch();
}
this is popentest1.cpp.....
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(" this is testing.... ");
cin>>a;
printf("%d",2*a);// this shud return 10....2*5
}
2. run popentest1.cpp initially to create the exe file. after that stop it.
1. run popentest2.cpp
now i want to read what was written in the stream of popentest1.exe and write into the stream of popentest1.exe...that is to be able to read from it and to supply arguments to popentest1.exe.
However i want it to be done without pressing enter. when we use cin, the prompt is waiting for enter to be pressed, plz help me, i want it automated that is after some parameter is written to popentest1.exe it should continue without waiting for enter to be pressed.
i dont mind using any other function but i need a solution to this. Please kindly help. if i pass '\n' as parameter also it is not working properly i think.
or to put it simply...
just give me 2 programs, i should be able to start one program from the other read from that program and write to it...using popen or anything else in vc++...just supplying the parameters and the programs shud not wait for "enter" . Even if u send '\n' parameter also its fine.
plz help.
S.
ASKER
run the 2 programs and you will know what my problem is.
give me the solution, i should not wait for enter anywhere in the program.
thanx for the early response though.....
give me the solution, i should not wait for enter anywhere in the program.
thanx for the early response though.....
ASKER
run the 2 programs and you will know what my problem is.
give me the solution, i should not wait for enter anywhere in the program.
thanx for the early response though.....
give me the solution, i should not wait for enter anywhere in the program.
thanx for the early response though.....
this is quite close to what u need..
u dont have to press enter to exit ..
the termination character is 'z'
when u enter 'z' popentest2 will exit and spit out whatever u typed till 'z'
popentest2.cpp
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define BUFSIZE 100
void main()
{
char* i="hello";
char buf[2];
char c;
FILE *fp;
fp = _popen("popentest1.exe","r +");
if(fp==NULL)
printf(" error reading file");
while ((c=getc(fp))!=EOF){
putc(c,stdout); fflush(stdout);
}
}
popentest1.cpp
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
char c;
printf(" this is testing.... ");
while((c=getch())!='z'){
printf(" %c",c);
}
}
u dont have to press enter to exit ..
the termination character is 'z'
when u enter 'z' popentest2 will exit and spit out whatever u typed till 'z'
popentest2.cpp
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define BUFSIZE 100
void main()
{
char* i="hello";
char buf[2];
char c;
FILE *fp;
fp = _popen("popentest1.exe","r
if(fp==NULL)
printf(" error reading file");
while ((c=getc(fp))!=EOF){
putc(c,stdout); fflush(stdout);
}
}
popentest1.cpp
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
char c;
printf(" this is testing.... ");
while((c=getch())!='z'){
printf(" %c",c);
}
}
Try this,
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define BUFSIZE 100
void main()
{
char* i="hello";
char buf[BUFSIZE];
FILE *fp;
fp = _popen("test5.exe","wrt");
if(fp==NULL)
printf(" error reading file");
while (fgets(buf, BUFSIZ, fp) != NULL)
(void) printf("%s", buf);
fputs("5 ",fp);
_pclose(fp);
}
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(" this is testing.... ");
cin>>a;
printf("%d",2*a);// this shud return 10....2*5
}
Exceter
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define BUFSIZE 100
void main()
{
char* i="hello";
char buf[BUFSIZE];
FILE *fp;
fp = _popen("test5.exe","wrt");
if(fp==NULL)
printf(" error reading file");
while (fgets(buf, BUFSIZ, fp) != NULL)
(void) printf("%s", buf);
fputs("5 ",fp);
_pclose(fp);
}
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(" this is testing.... ");
cin>>a;
printf("%d",2*a);// this shud return 10....2*5
}
Exceter
ASKER
thanx exceter,
i think i made a BLUNDER. it worked wonderfully as soon as i changed the mode.
thanx....well u get ur points.
thanx to akshayxx also but that wasnt quite what i wanted.
thnx for ur time.
i think i made a BLUNDER. it worked wonderfully as soon as i changed the mode.
thanx....well u get ur points.
thanx to akshayxx also but that wasnt quite what i wanted.
thnx for ur time.
You are most welcome.
I think had you put a sleep(3) after cin>>a you would have got the required results if I've understood your problem.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What srik wanted is that after you/ve read an input from user, the second process should take over and write back a '5' to the asme position. So by putting the process 1 to sleep, the second is sheduled to execute, wich overwrites the pipe.
But then after writing to the pipe, the process 2 should again call slepp, so that the first gain get a chance to execute and display the modifies result.
But then after writing to the pipe, the process 2 should again call slepp, so that the first gain get a chance to execute and display the modifies result.
Nothing has happened on this question in more than 10 months. It's time for cleanup!
My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Exceter.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Exceter.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
jmcg
EE Cleanup Volunteer
instead of cin use
getch in while loop like this
while(1){
c=getch();
putchar(c);
}