472,951 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 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 18307
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jean-Pierre Bergamin | last post by:
Dear python-Community We are forced to use a quite old simulation software that's based on Modula-2. The idea is now to let this software "talk" to the outside world over a TCP/IP network. ...
1
by: Vincent Touquet | last post by:
Hi, In a project where I have embedded Python in a C++ application, I have the need to replace what Python considers to be stdin, stdout and/or stderr. In sysmodule.c in the Python sources, I...
1
by: Harry George | last post by:
Normally the SOAP Servers are designed to take control of a port and run their own sockets via inheritance from SocktServer. But under inetd and xinetd, the port is controlled elsewhere and the...
1
by: Michael McGarry | last post by:
Hi, How do I redirect stdin, stdout and stderr to a window? I am using Qt for GUI. Thanks, Michael
5
by: Michael McGarry | last post by:
Hi, How can I redirect stdin/stdout to GUI widgets? Michael
3
by: Mike Finister | last post by:
Hi there At the moment I am writing a GUI front-end that in the background is to call some scripts on a Unix box. The GUI has to be written in Visual Basic (yes I said the VB word...sorry! :-) )...
5
by: Martijn Brouwer | last post by:
I am writing a unix daemon in python, so I want to close stdin, stdout and stderr. My first attempt was to the standard file descriptors using their close() methods. After closing stdout, I could...
2
momotaro
by: momotaro | last post by:
Hello! am wondring if stdin, stdout and stderr are of any help since we can do the same thing using 'printf' for example: fprintf(stdout, "%s", message); is exactely the same as: ...
8
by: subramanian100in | last post by:
The file stdio.h just contains extern FILE *stdin; extern FILE *stdout; extern FILE *stderr; When are the standard streams stdin, stdout, stderr initialiazed ? How are they initialiazed ? ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.