how to copy from std optput ?

W

wutongjoe

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!!
 
M

Mike Wahler

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()' said:
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
 
P

pankaj tiwary

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top