Help using pipes

This is a discussion on Help using pipes within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use pipes to write some data on a program stdin and get the stdout. I found this ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Help using pipes

    I'm trying to use pipes to write some data on a program stdin and get the stdout.

    I found this one on the internet and changed it, (the original didn't seems to work to):
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <iostream>
    #define WRITE 1
    #define READ 0
    
    using namespace std;
    
    int main()
    {
            char    buf[5];
            int     fd_in[2], fd_out[2],
                    c;
            pipe(fd_in);pipe(fd_out);
            if(fork() == 0)
            {
                    close(fd_in[WRITE]);dup2(fd_in[READ], READ);
            close(fd_out[READ]); dup2(fd_out[WRITE], WRITE);
    
            dup2(fd_out[WRITE], WRITE); //stdout -> pipe's write (everything what would go to stdout will go to the pipe's write...
                    execlp("sort", "sort", NULL);
            }
    
            write(fd_out[1], "c\n", 2);
            write(fd_out[1], "a\n", 2);
            write(fd_out[1], "b\n", 2);
    
            while((c = read(fd_in[0], buf, 5)) > 0)
                    write(1, buf, c);
    
            return  0;
    }
    I need to write some data on the sort's stdin and get the stdout, someone can help me?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are you closing the wrong end of the pipes in the child?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    now i'm using this code
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sstream>
    #include <iostream>
    #define READ 0
    #define WRITE 1
    
    using namespace std;
    
    int main()
    {
        int ficheiro_fd;
        int pipe_fd[2];
        char buffer[20], buffer2[20];
        int num_bytes;
        int infp;
        pipe(pipe_fd);
    
        switch ( fork() ) {
        case -1:
            exit(1);
        case 0:
            close(pipe_fd[1]);
            //close(pipe_fd[0]);
            dup2(pipe_fd[0], 0);
            
            execlp("/usr/bin/base64"," ", NULL);
            break;
        default:
            close(pipe_fd[0]);
            //dup2(pipe_fd[1], 1);
            //ficheiro_fd = open("output.txt", O_RDONLY);
        while ((num_bytes = read(fileno(stdin), buffer, 1)) > 0){
                write(pipe_fd[1], buffer, num_bytes);            
                }
                
                while ((num_bytes = read(pipe_fd[1], buffer, 1)) > 0){
                    //    write(fileno(stdout), buffer, num_bytes);
                    //    break;
                        }
            close(pipe_fd[1]);
            wait((int*)getpid());
        }
        
        
    
        return 0;
    }
    but i dont know how to get the data from the command, just how to send

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    FYI, in the future, please put these questions in the Linux forum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    but its not a c++ question?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    While that's true (though it mostly looks like C right now), it's still more of a Linux question. If your question involves how to use Linux APIs correctly, then it is unlikely that C++ gurus will be able to help you. It is far more likely that those skilled with Linux API can help you better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipes
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 03-31-2009, 03:09 AM
  2. IPC using PIPES
    By BMathis in forum C Programming
    Replies: 28
    Last Post: 03-15-2009, 09:47 AM
  3. Pipes!
    By NuNn in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 07:00 AM
  4. pipes
    By moi in forum C Programming
    Replies: 8
    Last Post: 08-11-2004, 01:59 PM
  5. Need some help with pipes please.
    By carrja99 in forum C Programming
    Replies: 1
    Last Post: 05-05-2004, 04:13 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21