Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-04-2004, 02:54 PM   #1
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.

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 :

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.

Old 07-04-2004, 03:53 PM   #2
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:


   dup2(wpipes[0], STDIN);
   dup2(rpipes[1], STDOUT);
   //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:

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

Original Poster
Rep: Reputation: 16

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

child: // just to make sure that child process continues.
QProcess *p = new QProcess( )

(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.

1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 PM.

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