470,647 Members | 1,061 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

how to copy from std optput ?

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!!
Jul 22 '05 #1
2 1342
<wu*******@hotmail.com> wrote in message
news:10***************@clint.its.waikato.ac.nz...
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
Jul 22 '05 #2
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<JK******************@newsread1.news.pas.eart hlink.net>...
<wu*******@hotmail.com> wrote in message
news:10***************@clint.its.waikato.ac.nz...
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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.