?
Solved

popen function.......

Posted on 2003-03-04
12
Medium Priority
?
363 Views
Last Modified: 2010-04-15
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.



0
Comment
Question by:srik27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
12 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8069646
dont want to wait for enter .. then use getch.. character by character

instead of cin use
getch in while loop like this

while(1){
c=getch();
putchar(c);
}
0
 

Author Comment

by:srik27
ID: 8069678
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.....
0
 

Author Comment

by:srik27
ID: 8069691
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.....
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:akshayxx
ID: 8070781
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);
}
}

0
 
LVL 8

Expert Comment

by:Exceter
ID: 8071261
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
0
 

Author Comment

by:srik27
ID: 8074682
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.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8075774
You are most welcome.
0
 

Expert Comment

by:adija
ID: 8087681
I think had you put a sleep(3) after cin>>a you would have got the required results if I've understood your problem.
0
 
LVL 8

Accepted Solution

by:
Exceter earned 300 total points
ID: 8088759
>> I think had you put a sleep(3) after cin>>a you would have got the required results if I've understood your problem.

Why is that?
0
 

Expert Comment

by:adija
ID: 8093679
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.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10088113
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question