471,071 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Using stdin and/or stdout between two programs

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
Nov 13 '05 #1
3 18148
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
Nov 13 '05 #2
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #3
> 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
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jean-Pierre Bergamin | last post: by
1 post views Thread by Vincent Touquet | last post: by
1 post views Thread by Harry George | last post: by
1 post views Thread by Michael McGarry | last post: by
5 posts views Thread by Michael McGarry | last post: by
3 posts views Thread by Mike Finister | last post: by
5 posts views Thread by Martijn Brouwer | last post: by
momotaro
2 posts views Thread by momotaro | last post: by
8 posts views Thread by subramanian100in | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.