From: Anderson Newsgroups: comp.os.msdos.djgpp Subject: Re: How to switch popen read/write modes? Date: Tue, 28 Nov 2000 20:48:53 -0200 Organization: Pennsylvania College of Technology Lines: 113 Message-ID: <3A243655.9539F8B@sigmanet.com.br> References: <3A2325FF DOT A38B511D AT sigmanet DOT com DOT br> <9003ep$s5t$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE> NNTP-Posting-Host: 200.245.19.211 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.7 [en] (Win95; I) X-Accept-Language: en To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Hans-Bernhard Broeker wrote: > > I'm trying out with popen function. It says (at help file) that it can > > open a program for reading and writing > > handle = popen ("program", "rb+") > > In what help file did you find that? It's untrue, for every popen() > implementation I've seen. The only 'mode' arguments for popen() are > "r" and ". I.e. the DJGPP docs may have a little flaw, here, where > they say: > > The MODE is the same as for `fopen' (*note fopen::.). > > That's not true. Oh, I think that's it, then. Just as you said, I've searched for popen, and help says the mode is the same as for fopen. So, I'm going to post the problem I was working on, maybe you guys can give some ideas (I'm counting on you! :). I was trying to control a "child" program within a "parent" one, sending to it messages and reading it's output, without the user even noticing the existence of "child". It worked fine for writing OR reading, but I couldn't figure it out why it was not working for both (thank you, now I know !) Here is the code: ================= child ================ // child.cpp: must be called within parent #include #include #include #include #include //ifstream, ofstream int main() { clrscr(); char buf[100]; ofstream out ("out.txt"); int a; cout<<"CHILD: Enter a number: "; cin >> a; cout <<"CHILD: Enter some words: "; gets (buf); // somebody said me I must use fgets -- why? // Sending results to screen cout << "\nCHILD: The squared number is: " << a*a << endl; cout << "CHILD: The words were: "<< buf; // Sending results to file "out.txt" out << "\nThe squared number is " << a*a < #include #include #include #include int main() { clrscr(); FILE* handle; char buf[100]; handle = popen("child", "wt+"); // + opens for both write/read. // Writing test: int & string int a; cout <<"PARENT: Enter a number: "; cin >> a; fprintf (handle,"%i\n",a); // \n = pushes ENTER into child cout << "PARENT: Enter some words: "; gets (buf); // fgets ? fprintf (handle, "%s\n", buf); // \n = pushes ENTER rewind (handle); // Prepares to change for reading // How can I read the output of child? // Testing reading while (fgets (buf,100,handle)) cprintf ("line obtained= %s", buf); getch(); pclose(handle); return 0; } ------------------------------------------- It's possible to solve this problem in a reasonable simple way? Thank you very much! Sincerely, Anderson.