How to send a simple string between two programs using pipes?

后端 未结 7 1118
渐次进展
渐次进展 2020-11-22 12:17

I tried searching on the net, but there are hardly any resources. A small example would suffice.

EDIT I mean, two different C programs communicating with each other.

7条回答
  •  星月不相逢
    2020-11-22 12:32

    dup2( STDIN_FILENO, newfd )
    

    And read:

    char reading[ 1025 ];
    int fdin = 0, r_control;
    if( dup2( STDIN_FILENO, fdin ) < 0 ){
        perror( "dup2(  )" );
        exit( errno );
    }
    memset( reading, '\0', 1025 );
    while( ( r_control = read( fdin, reading, 1024 ) ) > 0 ){
        printf( "<%s>", reading );
        memset( reading, '\0', 1025 );
    }
    if( r_control < 0 )
        perror( "read(  )" );    
    close( fdin );    
    

    But, I think that fcntl can be a better solution

    echo "salut" | code
    

提交回复
热议问题