The pipeline used in this article is the ' | ' in Linux that puts the output of the previous program into the last program's input. symbols, not the pipe of your own implementation
Code Listing 1: Program A.C output "HelloWorld", and the output is caught by B.C through the pipeline
A.C Code
#include <stdio.h>void main () {printf ("Hello world!:-P \ n");}
B.C Code
#include <stdio.h>void main () { char input[100]; char ch; int i = 0; while (ch = getchar ()) { if (ch != ' \ n ') { input[i++] = ch; } else { input[i] = '; ' break; } } printf (":-) %s\n", input);}
Compile and run, enter the command:
GCC a.c-o agcc b.c-o b./a |./b
Run effect
Code Listing 2: Program A.C input color as parameter, pass to B.C, output "Hello world!" of specified color
A.C Code
#include <stdio.h> #include <stdlib.h>void main (int argc, char *argv[]) {//Output main function received parameter int i; for (i = 1; i < argc; i++) {printf ("%s\n", Argv[i]); } printf ("end\n"); Exit (exit_success);}
B.C Code
#include <stdio.h> #include <stdlib.h> #include <string.h>void main () { char s[20]; while (1) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", s); if (strcmp (s, "Black") == 0) { printf ("\e[30;40;1m%s\e[37;40;0m\n", "Hello world! "); } else if (strcmp (s, "red") == 0) { printf ("\e[31;40;1m%s\e[37;40;0m\n", "Hello world! "); } else if (strcmp (s, "green") == 0) { printf ("\e[32;40;1m%s\e[37;40;0m\n", "Hello world! "); } else if (strcmp (s, "yellow") == 0) { printf ("\e[33;40;1m%s\e[37;40;0m\n", "Hello world! "); } else if (strcmp (s, "Blue") == 0) { printf ("\e[34;40;1m%s\e[37;40;0m\n", "Hello world! "); } else if (strcmp (s, "purple") == 0) { printf ("\e[35;40;1m%s\e[37;40;0 M\n ", " hello world! "); } else if (strcmp (s, "Darkgreen") == 0) { printf ("\e[36;40;1m%s\e[37;40;0m\n", " Hello world! "); } else if (strcmp (s, "white") == 0) { printf ("\e[37;40;1m%s\e[37;40;0m\n", "Hello world! "); } else if (strcmp (s, "End") != 0) { printf ("Hello World! (Unknown color) \ n "); } else { printf ("end!\n"); break; } } exit (exit_success);}
Compile and run, enter the command:
GCC a.c-o agcc b.c-o b./a parameters |./b
Run effect
END
Linux C program, through the simplest pipe (' | ' ) To implement data transfer between two programs