Using stdin and/or stdout between two programs

Discussion in 'C Programming' started by Harayasu, Aug 20, 2003.

  1. Harayasu

    Harayasu Guest

    Hi,

    Using fgets() I can read from stdin and with fputs() I can write to
    stdout. Now I have two programs, one writing to stdin and the other
    one reading from stdin. And I would like the second program to read
    the characters the first program has written to stdin, but I don't get
    it how to do this.

    The program which writes to stdin:

    #include <stdio.h>
    int main ()
    {
    char *string = "Hello\n";
    fputs (string, stdin);
    return 0;
    }

    The program which reads from stdin:

    #include <stdio.h>
    int main ()
    {
    char buffer [10];
    fgets (buffer, 10, stdin);
    printf ("%s", buffer);
    return 0;
    }

    Is it possible at all to use such a construction to read from stdin
    what an other program wrote to stdin? BTW, I'm using Linux.

    Regards,
    Harayasu
     
    Harayasu, Aug 20, 2003
    #1
    1. Advertising

  2. Harayasu

    Sven Semmler Guest

    Harayasu wrote:

    > fputs (string, stdin);

    ^^^^^
    Replace that by stdout.

    > BTW, I'm using Linux.


    The stdin/stdout mix-up was your problem. Test the source like this ...

    [sven@zeibig stdinout]$ tail out.c
    #include <stdio.h>
    int main ()
    {
    char *string = "Hello\n";
    fputs (string, stdout);
    return 0;
    }
    [sven@zeibig stdinout]$ tail in.c
    #include <stdio.h>
    int main ()
    {
    char buffer [10];
    fgets (buffer, 10, stdin);
    printf ("%s", buffer);
    return 0;
    }
    [sven@zeibig stdinout]$ gcc -Wall -O3 -pedantic -ansi -o out out.c
    [sven@zeibig stdinout]$ gcc -Wall -O3 -pedantic -ansi -o in in.c
    [sven@zeibig stdinout]$ ./out | ./in
    Hello
    [sven@zeibig stdinout]$

    HTH

    /Sven

    --
    Sven Semmler http://www.semmlerconsulting.com/
    GPG Fingerprint: 72CA E26D C2A3 1FEB 7AFC 10EA F769 A9A4 937F 5E67
     
    Sven Semmler, Aug 20, 2003
    #2
    1. Advertising

  3. Harayasu

    CBFalconer Guest

    Harayasu wrote:
    >
    > Using fgets() I can read from stdin and with fputs() I can write
    > to stdout. Now I have two programs, one writing to stdin and the
    > other one reading from stdin. And I would like the second program
    > to read the characters the first program has written to stdin,
    > but I don't get it how to do this.
    >
    > The program which writes to stdin:
    >
    > #include <stdio.h>
    > int main ()
    > {
    > char *string = "Hello\n";
    > fputs (string, stdin);

    ^^^^^
    That should be "stdout"

    > return 0;
    > }
    >
    > The program which reads from stdin:
    >
    > #include <stdio.h>
    > int main ()
    > {
    > char buffer [10];
    > fgets (buffer, 10, stdin);
    > printf ("%s", buffer);
    > return 0;
    > }
    >
    > Is it possible at all to use such a construction to read from
    > stdin what an other program wrote to stdin? BTW, I'm using Linux.


    This is OS dependant, but very common. On Linux, Unix, MSDos, a
    Windows command line, you would enter:

    writer | reader

    and reader will take its stdin from the stdout of writer.

    This has nothing to do with the C language, but is elementary OS
    usage. This sort of thing is fundamental to writing simple
    programs and connecting them via scripts.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 20, 2003
    #3
  4. > Now I have two programs, one writing to stdin and the other
    > one reading from stdin. And I would like the second program to read
    > the characters the first program has written to stdin, but I don't get
    > it how to do this.


    One thing that you've probably misinterpreted is that stdin/stdout are
    relative to processes. I.e., you should talk about "its" stdin and
    "its" stdout when talking about a process. You can think of
    stdin/stdout as a couple of connectors to the outer world given to any
    running process in your system. stdin can *only* be used to input, and
    stdout can *only* be used to output.

    So your question (to which you find answers in other posters' replies)
    should really be: how do I connect the first program's stdout to the
    second program's stdin (so that what the first program outputs to its
    stdout is input by the second programs from its stdin).

    HTH
    MC
     
    moongateclimber, Aug 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vincent Touquet
    Replies:
    1
    Views:
    627
    Adrian B.
    Sep 3, 2004
  2. Vincent  Touquet
    Replies:
    0
    Views:
    468
    Vincent Touquet
    Sep 6, 2004
  3. asdsd sir
    Replies:
    1
    Views:
    352
    Diez B. Roggisch
    Feb 16, 2006
  4. Replies:
    0
    Views:
    1,210
  5. Sarbjit singh
    Replies:
    2
    Views:
    397
    Nobody
    Jul 23, 2012
Loading...

Share This Page