Using stdin and/or stdout between two programs

H

Harayasu

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
 
S

Sven Semmler

Harayasu said:
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
 
C

CBFalconer

Harayasu said:
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.
 
M

moongateclimber

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top