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?

Harry

Recommended Answers

All 2 Replies

Can you post your code?

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"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.