Thread: question about piping to external programs

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    38

    question about piping to external programs

    I've been experimenting with piping to external programs in the last few days and have gain no grounds.

    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main(int argc, char *argv[])
    {   
        string aa = "test";
         echo -e aa | test2.exe;    // doesn't work
         system("aa | test2.exe "); //doesn't work either
         system("PAUSE");
        return EXIT_SUCCESS;
    }

    Code for test2.exe:
    Code:
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int main(int argc, char *argv[])
    {    
        string bb; 
        cout << "Please enter stuff: ";
        cin  >> bb;
        cout << bb; 
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    It would be nice if someone could tell me how to make a pipe. It seems quite simple from those tutorials, however I just can't seem to grasp it and make it work in my code.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    Thanks, trying to figure out the example code now.

    Code:
      if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
          exit( 1 );
    I don't really get the line above.

    I'm assuming it's referning to
    dir =Directory
    *=at
    .c =C drive
    /on= ??
    /p= ??
    r= The calling process can read the spawned command's standard output via the returned stream
    t = Open in text mode.

    What's /on and /p? I tired googling it but google doesn't read "/"

    if i want to run a program called "test2.exe" would it be
    pPipe = _popen( "dir *.c test2.exe", "rt" ))

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    The * represents a wildcard (0 or more characters). So "*.c" represents all filenames ending in ".c" (or all files with extension "c") in the current working directory.

    As for the other arguments to dir:
    Command Reference: dir - Microsoft
    /o[[:]<SortOrder>]
    Sorts the output according to SortOrder, which can be any combination of the following values:

    ...
    n By name (alphabetical)
    ...
    /p

    Displays one screen of the listing at a time. To see the next screen, press any key on the keyboard.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Communicating with external programs
    By punkrockguy318 in forum C Programming
    Replies: 5
    Last Post: 04-28-2004, 03:46 PM
  2. how do i run external programs from source?
    By tameritoke in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2003, 02:44 PM
  3. Call External programs
    By carnt in forum C Programming
    Replies: 7
    Last Post: 03-06-2002, 01:52 PM
  4. Call External Programs in C .
    By carnt in forum C++ Programming
    Replies: 0
    Last Post: 03-05-2002, 10:41 AM
  5. Running external programs
    By speedy in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2001, 05:50 PM