CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Pipe in unix

  1. #1

    Pipe in unix

    Hi,

    I did a search on this forum and on google but I'm not able to find what I'm looking for!

    When you're on a unix shell and want to look the number of occurance of the word "all" that are in a text, you'll do (one way to do it) :

    # cat myfile | grep all | wc -w

    * maybe the syntax is wrong but that's not the point *

    Now let's assume I'm programming 'wc' in C or C++ (both are ok).
    What I've found on google is 'popen' but it needs "cat myfile | grep all" as argument nb 1.

    How do I do to get this from the shell ?
    Maybe I'm thinking of it the wrong way.

    Any help would be appreciated!

    Regards

    -tux

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    This worked under Linux and Irix for me using g++.
    Note: I got it from the popen man page on my Irix
    system. I don't know where BUFSIZ comes from -
    must be one of the headers.

    Code:
    #include <cstdio>
    #include <cstdlib>
    
    int main()
    {
        char *cmd = "cat m.cpp | grep int | wc -w";
        char buf[BUFSIZ];
        FILE *ptr;
    
        if ((ptr = popen(cmd, "r")) != NULL)
        {
            while (fgets(buf, BUFSIZ, ptr) != NULL)
                              (void) printf("%s", buf);
        }
        return 0;
    }

  3. #3
    Philip thanx for your answer.

    The point is I specified "Now let's assume I'm programming 'wc' ".

    I don't want to get the output of "cat m.cpp | grep int | wc -w"

    How do wc get the output from "cat m.cpp | grep int" ?

    Regards

    -tux

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    I'm not sure I quite understand. Doesn't just using :

    Code:
    char *cmd = "cat m.cpp | grep int";
    in my sample code give you that output ?

  5. #5
    Excuse my english... Maybe I'm not clear.
    It's not my motherthongue.

    I am programming 'wc'.

    wc is a program that counts the numbers of words, line or caracters in a file.
    it can also be used through a pipe.

    Anyway here is the thing.. in my 'wc' program you advise me to put :

    char *cmd = "cat m.cpp | grep int";

    ok I can do it easily but what if the guys want to do "cat another.file | wc -c"

    file name changed and no more grep.

    Do you understand what I mean ?

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    As far as I understand it, the | piping works with standard input and standard output. So all you have to do is write a program that uses either scanf (for C) or cin (for C++) and then write the results using printf or cout.

    I once wrote a simple text database tool under Linux that used piping for combining operations.

    Like
    Code:
    column Customer Address | selectrows "Customer=Yves *" | column Address
    to get the addresses of customers with first name Yves. All I did was using scanf and printf.

  7. #7
    Join Date
    Oct 2002
    Location
    Sweden
    Posts
    9
    As Yves said, just use your standard input & output handles.
    Dont worry about using popen(), the shell will have the pipes (if any) setup for you,
    so just read from your stdin unless files have been specified on the command line.

  8. #8
    OK Thanx guys.

    For the moment I have no idea how to write the lines but I have to think about it.. I don't have much time at the moment (lot of stuff at work).

    Regards

    -tux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured