Thread: provide input to another program

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    provide input to another program

    hello-

    i have program that accepts input via getchar() and performs a task after receiving that input. i know i can redirect stdin like so: a.out < inputfile to give input, but i am looking for a different way.

    is there a way i can write a separate program to provide input for the first program? i know how to execute the first program, just not give it input, it doesn't take command line args.

    thanks for any help.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Is this linux or windows? Use a pipe. The second program can open a pipe to write to the first program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    a.out < inputfile
    cat inputfile | a.out
    Both do much the same.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    Use popen(), defined in stdio.h. For example, for cross platform sockets :
    Code:
    #include <stdio.h>
    #define BUFSIZE 100
    
    int main(void) {
      char buf[BUFSIZE];
    
      FILE * f = popen("telnet example.com 80", "r+");
      if(f) {
        fprintf(f, "GET / HTTP/1.0\r\n\r\n");
        fread(buf, sizeof(char), BUFSIZE, f);
        buf[BUFSIZE-1] = '\0';
    
        printf("Result: '%s'\n", buf);
    
        pclose(f);
        return 0;
      }
      return -1;
    }
    The above will print the telnet header and the start of the response from example.com to a HTTP request. (Warning, it's untested, I'm behind a proxy at the moment.)
    Last edited by pdc; 07-29-2005 at 08:21 AM.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    standard pipes are one-way.
    you cannot read and write to the pipe at the same time.
    If you have opened the pipe as "r", you cannot write to it.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    thanks for the help guys, i got it to work with pipes.

    it worked for me to do write-only and read the output on the screen.

    just a quick question, if pipes are one-way, can you set up two pipes for two-way? and how would you make sure you are communicating with the same process?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    AFAIK: popen() can create bidirectional pipes on OS X and BSD, but only unidirectional on Linux and Solaris.
    Last edited by pdc; 07-29-2005 at 10:27 AM.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    on unix, bidirectional streams could be created with
    socketpair() and fdopen().

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    9
    I'm new here, but I wonder if this will work on windows?

  10. #10
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Should do if it's in the standard library.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    popen() is standard, I don't think socketpair() is available on Windows as it is on UNIX.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    popen() is probably part of the POSIX standard, which is what Linux uses, but Windows generally uses the ANSI standard.

    [edit]url[/edit]

    [edit2]
    This function is NOT included in 'C Programming Language' (ANSI) but can be found in 'The Standard C Library' book.
    Taken from here.
    [/edit2]
    Last edited by dwks; 07-30-2005 at 12:08 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    Sorry, yeah, Windows calls it _popen(). It has much the same behaviour, except read-write mode is "rw" and not "r+".

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks
    popen() is probably part of the POSIX standard, which is what Linux uses, but Windows generally uses the ANSI standard.
    There is no ANSI C standard method for opening pipes. Just like there's no standard method for reading a keystroke.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's what my edit said.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  3. Input via the serial port for a C program
    By Anthony in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2005, 02:19 PM
  4. Program skipping some input
    By Dita in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2004, 03:08 PM