how to copy from std optput ?

Discussion in 'C++' started by wutongjoe@hotmail.com, Sep 9, 2004.

  1. Guest

    Hi all,

    I was writting 2 programs in c++ using linux.Say,one is app1 and another
    one is app2,the app1 will generate 100 random numbers and then print
    them on the standard output,and then the app2 needs to copy the numbers
    from the standard output to its int array by doing this way: $ app1|app2
    (a pipe ? I am a newbi,and not sure how to call that ).the app1 is
    finished and numbers seem ok,the problem is ,how does app2 copy from
    screen? like usual ? int main(int argc,char * argv[]) ? what special
    header file do i need ? and what function to use ?


    Thanks a lot guys!!
     
    , Sep 9, 2004
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hi all,
    >
    > I was writting 2 programs in c++ using linux.Say,one is app1 and another
    > one is app2,the app1 will generate 100 random numbers and then print
    > them on the standard output,and then the app2 needs to copy the numbers
    > from the standard output to its int array by doing this way: $ app1|app2
    > (a pipe ?


    This mechanism is often called a 'pipe', yes. But that's an
    operating system feature, not a language one. (Many operating
    systems, e.g. Unix and Windows, do have this 'pipe' facility.)

    > I am a newbi,and not sure how to call that ).the app1 is
    > finished and numbers seem ok,the problem is ,how does app2 copy from
    > screen? like usual ? int main(int argc,char * argv[]) ?


    Every C++ program must have a 'main()' function, yes. It
    may optionally specify command line arguments, yes. But this
    has nothing to do with i/o.

    > what special
    > header file do i need ?


    #include the headers which declare the functions you use
    (e.g. if you use 'printf()', #include <stdio.h> ).

    >and what function to use ?


    If your operating system supports this 'piping', all you need
    to do is write your output to 'stdout' ('printf()' writes to
    'stdout', other i/o functions let you specify which stream),
    and take your input from 'stdin' ('scanf()' reads from 'stdin').
    Exactly which i/o functions you use depends upon what you're doing.
    Just make sure you write to 'stdout', and read from 'stdin'.

    If this does not work, then pursue help in a group that discusses
    your particular operating system.

    -Mike
     
    Mike Wahler, Sep 9, 2004
    #2
    1. Advertising

  3. "Mike Wahler" <> wrote in message news:<JKM%c.10789$w%>...
    > <> wrote in message
    > news:...
    > > Hi all,
    > >
    > > I was writting 2 programs in c++ using linux.Say,one is app1 and another
    > > one is app2,the app1 will generate 100 random numbers and then print
    > > them on the standard output,and then the app2 needs to copy the numbers
    > > from the standard output to its int array by doing this way: $ app1|app2
    > > (a pipe ?

    >


    You don't have to worry about the redirection. app1 will normally
    write to its standard output (which could be a terminal) and app2 may
    read its input from its standard input (which could be the keyboard).
    What you have to worry about is the creation of a pipe whose one end
    will be connected to the standard output of app1 and the other end
    should be connected to the standard input of app2.

    To implement this pipe, you need not do anything at the language level
    but the OS provides you with the basic facility. You can simply do $
    app1 | app2
    This will connect the output of app1 to the input of app2.
     
    pankaj tiwary, Sep 9, 2004
    #3
    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.

Share This Page