开发者

How to use output of program A as input for B in bash?

That is, B has a gets that asks for input, A has a puts that outputs something.

Both A and B are C p开发者_JAVA技巧rograms.

How can I use the output of program A as input for B in bash?

What I tried is ./A |./B and ./B |./A, but neither works.

UPDATE

How does stuff in stdout of A goes to stdin of B for ./A|./B?


Here is an example to get you started:

/* a.c */
#include <stdio.h>
int main() {
    puts("This is a string");
    return 0;
}

Compile this as "a.out".

Here is the program that will catch the string from a.out:

/* b.c */
#include <stdio.h>
int main() {
    char line[1024];
    fgets(line,1023,stdin);
    printf("b.c: %s",line);
    return 0;
}

Compile this as "b.out".

Now run them together:

./a.out | ./b.out

The main principle for pipes to work is that you write to stdout and read stdin. Let me know if you need more help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜