LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2004, 02:54 PM   #1
C++Boar
Member
 
Registered: Mar 2004
Location: Denmark
Distribution: Gentoo
Posts: 68

Rep: Reputation: 16
popen read and write (both) how


How can I pipe / stream from and to another program.
With popen I can do one of the things but not both !

I would like a (GUI)-program I write to talk to a commandlineprogram.

Eg the commandline-programs ask you questions.

Eg
Do you want to continue (Y/N)

Ok this can be solved by "echo -n y| " in popen.

But if it is not known what the program will ask about.
And it maybe will ask you

"Are you sure you want to remove all files in this folder".

So my problem is :

FILE *f
f=popen("Command"","r"); // r ? I cant give rw (or r+)
while (fgets(tmpBuffer,BufferSize-1,f)!=NULL) // or fgetc
{
if (strcmp(Buffer,Choise1))
// now I want to do eg fputs("y\n",f) // but that is not possible.
}

How to get around this problem. It might be pipe, but the man page is not
understandable and seach on google just results in a lot about streaming in commandline. And pipeing with |

So I hope for an answer with a link if the answer is pipe.

/C++Boar
 
Old 07-04-2004, 03:53 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
create your own set of pipes, dup2 them onto stdin and out in the child, exec. pseudocode:

Code:
pipe(rpipes);
pipe(wpipes);

fork()
child:
   close(wpipes[1]);
   close(rpipes[0]);
   dup2(wpipes[0], STDIN);
   dup2(rpipes[1], STDOUT);
   close(wpipes[0]);
   close(rpipes[1]);
   exec(..)
parent:
   close(wpipes[0]);
   close(rpipes[1]);
   //write to stdin
   write(wpipes[1], ...);
   //read from stdout
   read(rpipes[0], ...);
when you create a set of pipes with the pipe call, you r creating a read pipe and a write pipe. w/e u write into the write pipe can be read from the read pipe. pipe[0] is for reading, pipe[1] is for writing. the parent and child share a file descriptor table, so when the child writes to pipe[1], the parent can read what child wrote from pipe[0]. the dup2 function is used to duplicate file descriptors. when u say dup2(fd1, fd2); fd2 is closed, and reopened on fd1. this means that any write to fd2 or fd1 will both be to the same descriptor. they both refer to the same entry in the process's file table, fd1. the effect of dup2(pipe[0], STDIN), is that the read end of the pipe now becomes standard input. so when the process goes to read from STDIN, it will actually be reading from the pipe. HTH, i'd advise picking up this book as well:
http://www.kohala.com/start/apue.html

Last edited by infamous41md; 07-04-2004 at 03:55 PM.
 
Old 07-05-2004, 01:19 PM   #3
C++Boar
Member
 
Registered: Mar 2004
Location: Denmark
Distribution: Gentoo
Posts: 68

Original Poster
Rep: Reputation: 16
Thanks!

You are a hero. I will consider buying the book, but I know some M$ programming, but I know Linux has a concept here I am not familiar with.

But hopefully I can use these powers to do some nice free linux programs.

When I use QT (which I prefer) I think I can also do a

fork()
child: // just to make sure that child process continues.
{
QProcess *p = new QProcess( )
p->
}

parent:
(nothing / just carry on).

But I like knowing stuff on a lower level than that anyway. And I will try to use what you have written. And in this and many others probably. What you have written is smarter ...

Thanks again.

C++Boar
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mounted read-write Fat32 partition suddenly becomes read-only?? hohead Linux - General 8 04-05-2007 12:17 PM
Example share in smb.conf doesn't work - read/write vs read only kleptophobiac Linux - Networking 0 09-01-2004 07:14 PM
how do I change the filesystem from read-only to read-write? les_chow Linux - Newbie 5 05-26-2004 03:14 PM
Open office read only, K-write read/write mode lwtvh Linux - Newbie 1 07-19-2003 11:33 AM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration