Linux C program, through the simplest pipe (' | ' ) To implement data transfer between two programs

Source: Internet
Author: User
Tags strcmp

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.