-1

I've gone through a vast quantity of Google links and questions on here in order to understand how pipping works but I can't seem to grasp the concept of making my stdout redirect to the stdin of my second program.

I am attempting to create a program that moves around characters of text, encrypts it and puts it into a string. My second program is supposed to take that string and decrypt it. I understand the functions that are needed to create a pipe but what I don't understand is how it is pointed to the next program, all the source code that I have looked at seems to have both the parent and child in it and not in a second program.

screwtraffic
  • 21
  • 1
  • 1
  • 4
  • 2
    Show us SOME code... – Mats Petersson May 07 '15 at 21:27
  • Stack Overflow is [a Q&A resource, not a help forum](http://meta.stackoverflow.com/a/92115/228805). It's for specific questions about coding that can have definite correct answers, not a place to ask open-ended questions seeking broad overviews, tutorials, introductory info, or general pointers about getting started. Please read the [Tour] page and the following advice on asking good questions: [[ask]], [[Writing the perfect question](http://tinyurl.com/stack-hints)]. – Adi Inbar May 07 '15 at 21:28
  • Is that necessary to use two different programs for this task? If I understand correct its easily can be implemented in one executable. – Ashot Khachatryan May 07 '15 at 21:28
  • Which language are you using, C or C++? For example, the C language does not have `std::string` nor `std::istream`. – Thomas Matthews May 07 '15 at 22:29
  • Are you looking to share data between two programs? Are you have one program launch another and share between the two? – Thomas Matthews May 07 '15 at 22:30
  • Your implementation will be easier if you store the output from the first program into a file. The file can then be opened by the second program. No need for piping. – Thomas Matthews May 07 '15 at 22:32
  • Sorry if this was the inappropriate place for this question. I am using c++ and am trying to share data between the two programs. I chose to do it this way so I can get a better understanding of piping. – screwtraffic May 07 '15 at 23:14

2 Answers2

2

Establishing a pipe between two programs' standard streams is a matter of how you start the programs. If one execs the other (fork()/execlp(), for example), then the parent creates the pipe with pipe(), and parent and child use dup2() to associate the pipe ends with the appropriate file descriptors (before performing the exec, in the child's case).

If neither program starts the other, then the program that does start them (normally a shell) must set up the pipe.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
2

Assuming you are using the shell in a unix based operating system the pipe | character is used to create this link. For example: I want to run program1 and redirect the standard output of program1 to program2 as its standard input.

program1 | program2

here is a linux example of getting the names of the directory contents using ls then piping that to head -3 to get the names of first three files. Then we pipe the three names to tail -1 to get the last one (or the name of the third file in the directory).

ls | head -3 | tail -1

David
  • 3,166
  • 2
  • 30
  • 51