Hi friends,

I want to implement code which can take input from some other application.
Like say for example

ls | less

Here, less is the application/utility which takes input from ls via pipe.
I want to implement the same in C.

I Did following things

  1. Created PIPES using pipe command
  2. close local input via close(0)
  3. use dup2(0,fd[0])
  4. then i'm trying to read from fd[0] abd printing on console.

When I pass like
echo "testing" | ./myapps

Nothing happens

Can anyone suggest what am i missing?


4 Years
Discussion Span
Last Post by Tumlee

When you pipe two programs together via the shell, it actually just pumps the stdout of the program on the left into the stdin of the program on the right. There is no real need for calling pipe() inside your program in this case.

For example, if I wrote the following program:

#include <stdio.h>
#include <ctype.h>

int main(void)
    int c;

    while((c = getchar()) != EOF)   //Gets a character from stdin, while there are some left.
        putchar(toupper(c));        //Make it uppercase, and then print it to stdout.

    return 0;

And then I named that program "MyUpper", I would just run it like so:

echo "testing" | ./MyUpper

And I would get the output "TESTING"

Edited by Tumlee

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.