473,503 Members | 5,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18352
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
4697
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
2592
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
3956
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
2818
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
2060
by: Michael McGarry | last post by:
Hi, How can I redirect stdin/stdout to GUI widgets? Michael
3
893
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
3532
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
3394
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
4579
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
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.